This guide explains how to integrate Refile’s PDF generation capabilities with the Vercel AI SDK in just a few lines of code. This integration allows AI models to generate PDFs directly from markdown content as part of their response flow.

Prerequisites

  • A refile API key (available from your refile dashboard)
  • Node.js project with Vercel AI SDK installed
  • Basic familiarity with Vercel AI SDK usage patterns

Quick Start

  1. Install the dependencies:
npm install ai @ai-sdk/openai zod
  1. Copy the refile tool code from refileTool.js:
import { openai } from "@ai-sdk/openai";
import { generateText } from "ai";
import { getRefileTool } from "./refileTool.js";

const { text } = await generateText({
	model: openai("gpt-4o"),
	prompt:
		"Create a research report on the situation in the rain forest. Reply with the URL of the resulting PDF.",
	maxSteps: 3,
	tools: {
		generatePdf: getRefileTool("<token>"),
	},
});

console.log(text);
  1. Replace <token> in index.js with your refile API key and start generating PDFs!

Customization Options

Want a different look? Just change these parameters when calling getRefileTool():

getRefileTool(
	"your-api-key",
	"academic", // theme: "modern", "basic"
	"right", // page numbers: "left", "center", "right", "none"
	"Letter" // page size: "A4", "Letter", "Legal", more
);

Remember to keep your API key safe by using environment variables in production and never exposing your key in client side code!

For a detailed explanation of the API and all configuration options, visit the API reference.