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, there are some complexities (such as polling for payment completion and document generation) that the SDK handles for you. First, install the SDK:
If you run your TypeScript or ES module file (for example, via node test.ts) and see a warning like Module type of file is not specified and it doesn’t parse as CommonJS, you can add a "type": "module" (or "type": "commonjs") in your package.json (or rename your file to .mjs) to remove the warning.
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”):
OfferDetails
type
A TypeScript type that describes the payment offer details. This type is used in the onPaymentRequest callback.
Example usage:
ConvertToPdfParams
type
A TypeScript type that describes the configuration options for the convertMarkdownToPdf function.
Example usage:
  • Markdown2PdfError – A class (extending Error) thrown (or used) for general conversion errors.
  • PaymentRequiredError – A class (extending Markdown2PdfError) thrown (or used) if a payment is required but no “onPaymentRequest” callback is provided.

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 optional object containing configuration parameters for the conversion process
If a Lightning payment is required, the SDK calls your onPaymentRequest callback so that you can handle the payment. If onPaymentRequest is omitted and a payment is required, a PaymentRequiredError is thrown.
The SDK retrieves the final PDF; either saving it to downloadPath or returning it as a Buffer if returnBytes is true.
Parameters:
markdown
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:
options.onPaymentRequest
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.
options.date
string
A string containing the date to use on the front cover. If not provided, the current date will be used.For example:
options.title
string
A string containing the title to use on the front cover. If not provided, a default will be used.For example:
options.downloadPath
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:
options.returnBytes
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:
options.apiUrl
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.