Skip to main content
This tutorial shows you how to build a Next.js chat where the user asks for a document (“a welcome letter for a new client”) and the bot replies with a link to the Word file. The agent calls Doconda from your server, so the API key never reaches the browser.

What you need

  • A Next.js project with the App Router, on Node 20 or later.
  • A Doconda API key in DOCONDA_API_KEY. Create one in the dashboard, under API keys.
  • Your model provider’s key. This tutorial uses Anthropic (ANTHROPIC_API_KEY); any AI SDK provider works.

Build the chat

1

Install the dependencies

2

Create the chat route

doconda.tools() returns Doconda’s tools (create_document, edit_document, convert_file, upload_file, get_document…), each with a name, a description, a JSON Schema and run(). Wrap each one as an AI SDK tool and pass them to streamText. Create app/api/chat/route.ts:
app/api/chat/route.ts
3

Create the chat page

useChat sends the messages to /api/chat and renders the reply as it arrives. Replace app/page.tsx:
app/page.tsx
4

Run it

Open http://localhost:3000 and type “Make me a welcome letter for a new client”.

Result

The model calls create_document with format: "docx". The tool waits until the file is ready and returns something like this to the model:
The bot’s reply carries the links to the Word file and its PDF. They expire after 5 minutes; if the user comes back later, the agent calls get_document and gets fresh ones.
create_document waits up to about 4 minutes for the finished document: about a minute at quality: "fast", a few minutes at standard. If it takes longer, it returns the id and status, and the model checks later with get_document. If your host limits how long a request may run, raise that limit for this route to 300 s.
When a tool call fails, run() doesn’t throw: it returns { error, message } (plus fields on a validation error), so the model can read the problem and tell the user.

Next steps

Use the AI SDK guide

The full integration, also with the MCP client.

Call the TypeScript SDK directly

Every method, to call Doconda without an agent.

Browse the examples

Complete, runnable projects on GitHub.