> ## 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.

# How to generate files in a Vercel AI SDK chatbot

> Pass doconda.tools() to streamText in your Next.js route: the bot creates Word, PowerPoint, Excel or PDF files and replies with the link.

Install `ai` and `@doconda/sdk`, turn `doconda.tools()` into AI SDK tools with `tool()` and `jsonSchema()`, and pass
them to `streamText` in your chat route. When the user asks for a document, the model calls `create_document` and its
answer carries the download link, which `useChat` renders in the UI.

## Steps

1. Put your Doconda API key (dashboard → **API keys**) in `DOCONDA_API_KEY` and your model provider's key (here,
   `ANTHROPIC_API_KEY`; any AI SDK provider works).
2. Install the dependencies: `npm install ai @ai-sdk/anthropic @ai-sdk/react @doconda/sdk`.
3. Create the chat route (server side) and the page:

<CodeGroup>
  ```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 and give them the download link.",
      messages: await convertToModelMessages(messages),
      tools,
      stopWhen: isStepCount(5),
    })
    return result.toUIMessageStreamResponse()
  }
  ```

  ```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>
    )
  }
  ```
</CodeGroup>

The agent calls Doconda from your server, so the API key never reaches the browser. Use the SDK on the server only.

## What you get back

The tool returns this summary to the model, and the model writes the link into its answer:

```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."
}
```

* `links`: the file (`docx`, `pptx` or `xlsx`) and its PDF, or the PDF alone with `format: "pdf"`.
* Links expire after 5 minutes; if the user comes back later, the agent calls `get_document` and gets fresh ones.
* `status`: `ready`, `ready_with_warnings`, `needs_review` or `failed` (not charged).

## FAQ

<AccordionGroup>
  <Accordion title="Can I show the document being written on my site?">
    Yes, without exposing the key: see [Watch the document live](/en/sdk/stream).
  </Accordion>

  <Accordion title="Does it work with the AI SDK MCP client?">
    Yes: `createMCPClient` from `@ai-sdk/mcp` against `https://api.eu.doconda.com/mcp` with your key in `Authorization`.
  </Accordion>

  <Accordion title="Does it work with OpenAI, Gemini or Bedrock?">
    Yes, change the `model`: the tools are the same with any AI SDK provider.
  </Accordion>

  <Accordion title="How much does it cost?">
    You pay per delivered document. See [pricing](/en/guides/account#pricing).
  </Accordion>
</AccordionGroup>

## Keep reading

* [AI SDK](/en/agents/ai-sdk): the full guide.
* [A Vercel AI SDK chatbot that returns a Word file](/en/tutorials/ai-sdk-chatbot-word): the step-by-step tutorial.
* [Quickstart](/en/quickstart).


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