Getting started
github.com/Serendipity-AI/markdown2pdf-python Our Python SDK makes it easy to use markdown2pdf.ai in your agentic workflows. Whilst using our REST APIs is straightforward, there are some complexities to handle around polling for payment completion and document generation that the SDK avoids you needing to worry about. First up, you’ll need to install the SDK:on_payment_request function is called when payment is required, allowing you to handle the payment process in your application. There are several ways of handling payments automatically, such as fewsats.com or albyhub or using a Lightning wallet that supports automatic payments. In the example above, we simply print the payment details and wait for you to confirm payment manually. You can simply copy and paste the lightning invoice into your lightning wallet to pay.
SDK Reference
class MarkdownPDF
The main class for interacting with the markdown2pdf.ai service.
constructor
Used for initializing the MarkdownPDF client.
function
default:"None"
The SDK calls this function when the service asks for payment. It receives an The
offer dict containing details about the payment, such as amount, currency, description, and the Lightning invoice. You can implement your own logic to handle payments here, and the function can be a plain function or an async one. The service uses the L402 protocol, so a new document needs a payment before the service generates the PDF. If the service asks for payment and you haven’t supplied a handler, convert raises PaymentRequiredException("Payment required but no handler provided.").offer dict looks like this. amount is in US cents, so 1 means one cent; the Lightning invoice in payment_request carries the equivalent in sats.string
default:"https://api.markdown2pdf.ai"
This parameter enables you to override the default API URL. This is typically used in development or testing environments, but for normal usage you won’t need to touch it: omit it and the client uses
https://api.markdown2pdf.ai. Don’t pass None, as the client would then build an invalid URL.number
default:"3"
The number of seconds the client waits between polling requests, both after calling your payment handler and while the service generates the PDF. The client polls for the finished document up to 10 times (about 30 seconds at the default interval), then raises
Markdown2PDFException.convert
Converts the provided markdown content into a PDF document. This method handles the payment process automatically if required, and either saves the PDF to download_path, returns it as bytes, or returns its download URL.
string
required
A string containing the markdown content you want to convert to PDF.Typically your markdown will come from the output of an LLM or AI agent. If you want to load some from a file, you can do it like this:
string
A string containing the date to use on the front cover. If not provided, the current date will be used.
string
A string containing the title to use on the front cover. If not provided, a default will be used.
string
A string containing the path to use for saving the PDF file. When you provide it,
convert writes the PDF there and returns the path. If you provide neither download_path nor return_bytes=True, convert returns the PDF’s download URL as a string.For example:boolean
Whether or not to return the PDF as bytes instead of saving it to a file. If set to
True, the method returns the PDF content as a byte string and doesn’t write a file. If set to False (the default), the method saves the PDF to download_path and returns that path, or returns the download URL if you didn’t provide a path.For example:class AsyncMarkdownPDF
An async version of MarkdownPDF with the same constructor and convert parameters. Use it when your code already runs in an event loop, and await the call:
Automating payments
If you want to automate payments, you can use a service like fewsats.com or Albyhub for Lightning, or any Solana wallet that holds USDC for X402. These services let you pay programmatically, completely headlessly.- Albyhub
- lnbits
- Fewsats
- Coinbase X402

