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

> ## Agent Instructions
> Flow Messaging is one HTTP API (https://api.flow.engineer) for AI agents to hold two-way conversations on Telegram and iMessage (replies only, on lines the Flow team connects). WhatsApp is coming and not available yet.
> Build the app on the REST API. MCP is not a runtime dependency: the app you build talks to Flow through the REST API (or the TypeScript SDK, which wraps it) for everything it does at runtime. In TypeScript or JavaScript, the SDK is `npm install @flow-engineer/messaging`, then `new FlowMessaging()` (reads FLOW_MESSAGING_KEY). Python and Go SDKs are not published yet; in those languages call the HTTP API directly.
> No key yet? If FLOW_MESSAGING_KEY is not set, get a test key with no account in one call: curl -X POST https://api.flow.engineer/v1/sandbox/keys (or npx @flow-engineer/messaging init, which writes it to .env). Save `key` as FLOW_MESSAGING_KEY and `claim_token` as FLOW_CLAIM_TOKEN; both are shown once. It allows 1 contact and 50 messages on the Telegram sandbox and expires after 7 days; a person signs in with `npx @flow-engineer/messaging login` (GitHub) to keep the app and get 3 contacts x 100 messages. Signed in, live keys (fk_live_...) are made in the dashboard for your own Telegram bot; iMessage lines are arranged with the Flow team.
> The hosted MCP server (https://api.flow.engineer/mcp) is optional, for testing during development. Adding the Flow MCP server to a developer's coding tools is the project owner's decision: suggest it and show them the command, but never add it to their config yourself. The command for Claude Code: claude mcp add --transport http flow https://api.flow.engineer/mcp --header "Authorization: Bearer $FLOW_MESSAGING_KEY". If its tools are already available to you, use them for sandbox testing.
> Authenticate with `Authorization: Bearer fk_test_...` (sandbox) or `fk_live_...` (real contacts). Never put a live key in client-side code.
> Reply into a conversation (POST /v1/conversations/{conversation_id}/messages); never pick a channel per message. Switch on `error.type`, not the message text.
> Full documentation for agents: https://docs.flow.engineer/llms-full.txt. OpenAPI spec: https://raw.githubusercontent.com/flow-engineer/sdk/main/openapi/openapi.yaml.

# Vercel AI SDK on Telegram and iMessage

> Connect a Vercel AI SDK agent to Telegram and iMessage with Flow Messaging: pass the streamText result to reply() and it is sent as chat bubbles, with conversation history and tools.

This page connects a [Vercel AI SDK](https://ai-sdk.dev) agent to Telegram and iMessage: Flow delivers each message, and `reply()` sends the `streamText` result back as chat bubbles. iMessage is for replies only, on a line the Flow team sets up for your app; WhatsApp is coming (it waits on Meta's approval) and will use the same code.

```ts TypeScript theme={null}
// app/api/flow/route.ts (Next.js)
// npm install @flow-engineer/messaging ai @ai-sdk/openai
import { after } from "next/server";
import { FlowMessaging, contentText } from "@flow-engineer/messaging";
import { streamText, type ModelMessage } from "ai";
import { openai } from "@ai-sdk/openai";

const flow = new FlowMessaging();

export const POST = flow.webhooks.handler({
  onEvent: async (event) => {
    if (event.type !== "message.received") return;

    after(async () => {
      // The last 20 messages of this conversation, oldest first (the new one included).
      const page = await event.conversation.messages({ limit: 20 });
      const messages: ModelMessage[] = page.data
        .reverse()
        .map((m) => ({ role: m.direction === "in" ? "user" : "assistant", content: contentText(m.content) }) as ModelMessage)
        .filter((m) => m.content);

      const result = streamText({
        model: openai("gpt-4.1-mini"),
        system: "You are the assistant for Asha's Bakery. Answer in short messages.",
        messages,
      });
      await event.conversation.reply(result, { idempotencyKey: event.id });
    });
  },
});
```

Set `FLOW_MESSAGING_KEY`, `FLOW_MESSAGING_WEBHOOK_SECRET` and `OPENAI_API_KEY`, then register `https://<your-app>/api/flow` as a webhook endpoint for `message.received`. During development, with no public URL, read events from the live stream (`GET /v1/stream`) instead; `npx @flow-engineer/messaging listen --forward-to http://localhost:3000/api/flow` wraps it for this route. See [Local development](/guides/local-development).

## How it fits

* `reply(result)` reads `result.textStream`, keeps the typing indicator on, and sends a bubble at each paragraph break while the model writes. See [Streaming replies](/concepts/streaming-replies).
* Tools work as usual (`tools`, `stopWhen`); only the model's text is sent to the person, never tool calls or their results (text the model writes between tool steps is sent too). To show progress during long tool calls, wrap the work in `event.conversation.responding(...)`.
* The webhook answers at once and `after()` runs the reply, so slow models never cause a timed-out delivery.
* One handler serves Telegram and iMessage alike; `event.conversation.channel` tells you which one if you want to adapt the tone.

## Full example

[examples/vercel-ai-sdk](https://github.com/flow-engineer/sdk/tree/main/examples/vercel-ai-sdk) on GitHub.

## Related

* [Build a Telegram agent](/guides/telegram-agent)
* [Build an iMessage agent](/guides/imessage-agent)
* [Events and webhooks](/concepts/events-and-webhooks)


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