> ## Documentation Index
> Fetch the complete documentation index at: https://docs.refile.co/llms.txt
> Use this file to discover all available pages before exploring further.

# Vercel AI SDK

> Use refile with Vercel AI SDK

This guide explains how to integrate Refile's PDF generation capabilities with the [Vercel AI SDK](https://sdk.vercel.ai/docs) 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](https://refile.co/dashboard))
* Node.js project with Vercel AI SDK installed
* Basic familiarity with Vercel AI SDK usage patterns

## Quick Start

1. Install the dependencies:

```bash theme={null}
npm install ai @ai-sdk/openai zod
```

2. Copy the refile tool code from refileTool.js:

```javascript index.js theme={null}
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);
```

```javascript refileTool.js theme={null}
import { z } from "zod";
import { tool } from "ai";

/**
 * Creates a tool for generating PDFs from markdown text using the Refile API
 *
 * @param {string} refileApiKey - Your Refile API key
 * @param {string} [theme="modern"] - PDF theme (e.g., "modern", "minimal", "academic")
 * @param {string} [pageNumeration="center"] - Page number position ("center", "right", "none")
 * @param {string} [pageSize="A4"] - Page size (e.g., "A4", "Letter", "Legal")
 * @param {string} [refileApiBaseUrl="https://www.refile.co/api/v1"] - Refile API base URL
 * @returns {Object} - AI SDK compatible tool for PDF generation
 */
export function getRefileTool(
	refileApiKey,
	theme = "modern",
	pageNumeration = "center",
	pageSize = "A4",
	refileApiBaseUrl = "https://www.refile.co/api/v1"
) {
	return tool({
		description: "Generate a PDF from a markdown text",
		parameters: z.object({
			markdown: z.string().describe("The markdown to generate a PDF from"),
		}),
		execute: async ({ markdown }) => {
			// Prepare request options
			const options = {
				method: "POST",
				headers: {
					"Content-Type": "application/json",
					Authorization: `Bearer ${refileApiKey}`,
				},
				body: JSON.stringify({
					markdown: markdown,
					theme: theme,
					pageNumeration: pageNumeration,
					pageSize: pageSize,
					output: "url",
				}),
			};

			try {
				// Make the API call
				const response = await fetch(
					`${refileApiBaseUrl}/pdf/markdown`,
					options
				);

				if (!response.ok) {
					const errorData = await response.json().catch(() => null);
					throw new Error(
						`Failed to generate PDF: ${response.status} ${response.statusText}${
							errorData ? ` - ${JSON.stringify(errorData)}` : ""
						}`
					);
				}

				// Parse and return the response
				const data = await response.json();
				return {
					url: data.url,
					success: true,
					message: "PDF generated successfully",
				};
			} catch (error) {
				console.error("Error generating PDF:", error);
				return {
					success: false,
					error: error.message,
					message: "Failed to generate PDF",
				};
			}
		},
	});
}
```

3. 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()`:

```javascript theme={null}
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](https://docs.refile.co/api-reference).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.