Skip to main content

Getting started

github.com/Serendipity-AI/markdown2pdf-typescript Our TypeScript SDK makes it easy to use markdown2pdf.ai in your agentic workflows. Whilst using our REST APIs is straightforward, the SDK handles the fiddly parts for you: it fetches a Lightning invoice, passes it to your payment callback, re-submits your request once the callback returns, and polls until your PDF is ready. First, install the SDK:
The SDK ships as an ES module only, and the examples on this page use ES module syntax, so Node must load your code as an ES module. Add "type": "module" to your package.json (or give your file the .mts extension). This also removes the warning Module type of file is not specified and it doesn’t parse as CommonJS that Node prints when you run a TypeScript file directly (for example, via node test.ts).
With that done, you can try a sample code snippet:
This code converts your markdown into a PDF. If a Lightning payment is required, the SDK calls your onPaymentRequest callback (here our pay function) so that you can handle the payment (for example, by printing the invoice and waiting for manual confirmation). You can also integrate with services like fewsats.com or albyhub for automatic payments.

SDK Reference

Types

The SDK exports the following types (and classes) (from “@serendipityai/markdown2pdf-typescript”):
type
A TypeScript type that describes the payment offer details. This type is used in the onPaymentRequest callback.
Example usage:
type
A TypeScript type that describes the configuration options for the convertMarkdownToPdf function.
Example usage:
  • Markdown2PdfError – A class (extending Error) for conversion errors. The errors the SDK throws are not instances of the exported class, so instanceof Markdown2PdfError returns false; check error.name === "Markdown2PdfError" instead:
  • PaymentRequiredError – A class (extending Markdown2PdfError). The SDK exports it but never throws it.

Function: convertMarkdownToPdf

Signature:
Description: Converts the provided markdown (a string) into a PDF. The function takes two parameters:
  1. markdown: The markdown content to convert
  2. options: An object containing configuration parameters for the conversion process, including the required onPaymentRequest callback
If you omit onPaymentRequest, the SDK throws a Markdown2PdfError (“Payment required but no handler provided.”) before it sends any request. When the API asks for payment, the SDK fetches a Lightning invoice and calls your callback with it. The SDK waits up to 300 seconds for your callback to resolve, then re-submits the conversion request. If the invoice is still unpaid, the SDK fetches a new invoice and calls your callback again. Once the API accepts the request, the SDK checks the job status every 3 seconds, for up to 300 seconds.
The SDK retrieves the final PDF; either saving it to downloadPath or returning it as a Buffer if returnBytes is true.
Parameters:
string
required
A string containing the markdown content you want to convert to PDF.
A note on markdown hashingmarkdown2pdf.ai uses a hashing mechanism to ensure that you won’t get charged twice for creating the same PDF. Subsequent requests to convert the same markdown content will return the previously generated PDF without requiring a new payment.
For example:
function
required
A callback function that is called when a Lightning payment is required. The function receives an offer object containing payment details. This parameter is required because the service uses the L402 protocol, which requires a payment to be made before generating the PDF.
string
A string containing the date to use on the front cover. If not provided, the current date will be used.For example:
string
A string containing the title to use on the front cover. If not provided, a default will be used.For example:
string
A string containing the path to use for saving the PDF file. If not provided and returnBytes is false, the PDF URL will be returned instead.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 will return the PDF content as a Buffer. If set to false, it will save the PDF to the specified downloadPath or return the URL if no path is provided.For example:
string
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 and it can be set to undefined or omitted.
Returns The function always returns a Promise that resolves or rejects as described above.

Automating payments

If you want to automate Lightning payments (so that your “onPaymentRequest” callback pays the invoice automatically), you can integrate with a service (or Lightning wallet) such as fewsats.com or albyhub. For X402 (USDC on Solana) use any Solana wallet that holds USDC — see the X402 tab below.