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

# A Vercel AI SDK chatbot that returns a Word file

> How to build a Next.js chatbot with the Vercel AI SDK that creates Word documents (.docx) and replies with the download link.

You'll build a chat in Next.js: the user asks for a document ("a welcome letter for a new client") and the bot replies
with the link to download 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 (Node 20+).
* A Doconda API key in `DOCONDA_API_KEY`. Create it in the dashboard, under **API keys**.
* Your model provider's key. Here we use Anthropic (`ANTHROPIC_API_KEY`); any other AI SDK provider works.

## 1. Install the dependencies

```bash theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
npm install ai @ai-sdk/anthropic @ai-sdk/react @doconda/sdk
```

## 2. Create the chat route

`doconda.tools()` gives you Doconda's tools (create, upload, review, edit…). You turn them into AI SDK tools and pass
them to `streamText`.

```ts app/api/chat/route.ts theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
import { anthropic } from "@ai-sdk/anthropic"
import { convertToModelMessages, isStepCount, jsonSchema, streamText, tool, type UIMessage } from "ai"
import { Doconda } from "@doconda/sdk"

const doconda = new Doconda()
const tools = Object.fromEntries(
  doconda.tools().map((t) => [
    t.name,
    tool({ description: t.description, inputSchema: jsonSchema(t.inputSchema), execute: (input) => t.run(input as Record<string, unknown>) }),
  ]),
)

export async function POST(req: Request) {
  const { messages }: { messages: UIMessage[] } = await req.json()
  const result = streamText({
    model: anthropic("claude-sonnet-5-5"),
    system: "If the user asks for a document, create it as Word (docx) and give them the download link.",
    messages: await convertToModelMessages(messages),
    tools,
    stopWhen: isStepCount(5),
  })
  return result.toUIMessageStreamResponse()
}
```

## 3. Create the chat page

`useChat` sends the messages to `/api/chat` and renders the reply as it arrives.

```tsx app/page.tsx theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
"use client"

import { useChat } from "@ai-sdk/react"
import { useState } from "react"

export default function Chat() {
  const { messages, sendMessage } = useChat()
  const [input, setInput] = useState("")

  return (
    <main>
      {messages.map((m) => (
        <p key={m.id}>
          <b>{m.role}:</b> {m.parts.map((p) => (p.type === "text" ? p.text : "")).join("")}
        </p>
      ))}
      <form
        onSubmit={(e) => {
          e.preventDefault()
          sendMessage({ text: input })
          setInput("")
        }}
      >
        <input value={input} onChange={(e) => setInput(e.target.value)} placeholder="Ask for a document…" />
      </form>
    </main>
  )
}
```

## 4. Try it

```bash theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
npm run dev
```

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

## Result

The agent calls `create_document`, waits until the Word file is ready and gets this back:

```json theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
{
  "id": "doc_01JAB3…",
  "format": "docx",
  "status": "ready",
  "links": { "docx": "https://…", "pdf": "https://…" },
  "links_note": "Links expire in 5 minutes: call get_document for fresh ones."
}
```

The bot's reply contains the link to the Word file (`.docx`) and to its PDF. The links expire after 5 minutes; if the
user comes back later, the agent calls `get_document` and gets new ones.

## Next steps

* [AI SDK](/en/agents/ai-sdk): the full guide, also with the MCP client.
* [TypeScript SDK](/en/sdk/introduction): every method, in case you want to call Doconda without an agent.
* [Complete examples on GitHub](https://github.com/B-C-Software/doconda-examples).


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