Skip to content
Cloudflare Docs

Workers Binding

Cloudflare’s serverless platform allows you to run code at the edge to build full-stack applications with Workers. A binding enables your Worker or Pages Function to interact with resources on the Cloudflare Developer Platform.

To use our Markdown Conversion service directly from your Workers, create an AI binding either in the Cloudflare dashboard (refer to AI bindings for instructions), or you can update your Wrangler file. Add the following to your Wrangler file:

{
"$schema": "./node_modules/wrangler/config-schema.json",
"ai": {
"binding": "AI"
}
}

Examples

Converting files

In this example, we fetch a PDF document and an image from R2 and feed them both to env.AI.toMarkdown. The result is a list of converted documents. Workers AI models are used automatically to detect and summarize the image.

JavaScript
import { Env } from "./env";
export default {
async fetch(request, env, ctx) {
// https://pub-979cb28270cc461d94bc8a169d8f389d.r2.dev/somatosensory.pdf
const pdf = await env.R2.get("somatosensory.pdf");
// https://pub-979cb28270cc461d94bc8a169d8f389d.r2.dev/cat.jpeg
const cat = await env.R2.get("cat.jpeg");
return Response.json(
await env.AI.toMarkdown([
{
name: "somatosensory.pdf",
blob: new Blob([await pdf.arrayBuffer()], {
type: "application/pdf",
}),
},
{
name: "cat.jpeg",
blob: new Blob([await cat.arrayBuffer()], {
type: "image/jpeg",
}),
},
]),
);
},
};

Getting supported file formats

JavaScript
import { Env } from "./env";
export default {
async fetch(request, env, ctx) {
return Response.json(await env.AI.toMarkdown().supported());
},
};

Methods

async env.AI.toMarkdown()

Takes a document or list of documents in different formats and converts them to Markdown.

JavaScript
const result = await env.AI.toMarkdown({
name: "document.pdf",
blob: new Blob([documentBuffer]),
});

Parameter

  • files: MarkdownDocument | MarkdownDocument[]- an instance of or an array of MarkdownDocuments.

  • conversionOptions: ConversionOptions- options that control how conversion happens. See Conversion Options for further details.

Return values

  • results: Promise<ConversionResult | ConversionResult[]>- An instance of or an array of ConversionResults.

MarkdownDocument definition

  • name string

    • Name of the document to convert.
  • blob Blob

    • A new Blob object with the document content.

ConversionResult definition

  • id string

    • ID associated to this object.
  • name string

    • Name of the converted document. Matches the input name.
  • format 'markdown' | 'error'

    • The format of this ConversionResult object
  • mimetype string

  • tokens number

    • The estimated number of tokens of the converted document. Only present if format is equal to markdown.
  • data string

    • The content of the converted document in Markdown format. Only present if format is equal to markdown.
  • error string

    • The error message explaining why this conversion failed. Only present if format is equal to error.

async env.AI.toMarkdown().transform()

This method is similar to env.AI.toMarkdown except that it is exposed through a new handle. It takes the same arguments and returns the same values.

JavaScript
const result = await env.AI.toMarkdown().transform({
name: "document.pdf",
blob: new Blob([documentBuffer]),
});

async env.AI.toMarkdown().supported()

Returns a list of file formats that are currently supported for markdown conversion. See Supported formats for the full list of file formats that can be converted into Markdown.

JavaScript
const formats = await env.AI.toMarkdown().supported();

Return values

  • results: SupportedFormat[]- An array of all formats supported for markdown conversion.

SupportedFormat definition

  • extension string

    • Extension of files in this format.
  • mimeType string