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

# LangChain agents on Telegram and iMessage

> Connect a LangChain chat model or chain to Telegram and iMessage with Flow Messaging: pass model.stream() to reply() in TypeScript, or answer from the webhook in Python.

This page connects a [LangChain](https://js.langchain.com) model or chain to Telegram and iMessage: Flow delivers each message and `reply()` sends the streamed chunks 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.

<CodeGroup>
  ```ts TypeScript theme={null}
  // npm install @flow-engineer/messaging @langchain/openai
  import { ChatOpenAI } from "@langchain/openai";
  import { FlowMessaging, contentText } from "@flow-engineer/messaging";

  const flow = new FlowMessaging();
  const model = new ChatOpenAI({ model: "gpt-4.1-mini" });

  for await (const event of flow.events.stream({ types: ["message.received"] })) {
    const stream = await model.stream([
      ["system", "You are the assistant for Asha's Bakery. Answer in short messages."],
      ["human", contentText(event.data.message.content)],
    ]);
    await event.conversation.reply(stream); // reads each chunk's content
  }
  ```

  ```python Python (webhook reply) theme={null}
  # pip install langchain-openai fastapi
  # There is no Python SDK yet (only TypeScript is published); this answers in the webhook response.
  from fastapi import FastAPI, Request
  from langchain_openai import ChatOpenAI

  model = ChatOpenAI(model="gpt-4.1-mini")
  app = FastAPI()

  @app.post("/flow")
  async def flow_webhook(request: Request):
      event = await request.json()  # verify Flow-Signature first: see Events and webhooks
      if event["type"] != "message.received":
          return {}
      text = event["data"]["message"]["content"].get("text", "")
      answer = await model.ainvoke([("system", "You are the assistant for Asha's Bakery."), ("human", text)])
      return {"reply": {"type": "text", "text": answer.content, "format": "markdown"}, "fallback": "auto"}
  ```
</CodeGroup>

## How it fits

* Any runnable whose `.stream()` yields message chunks works with `reply()`: chat models, `prompt.pipe(model)` chains, and chains ending in a string output parser.
* The Python version answers in the webhook response (one round trip). Flow waits 10 seconds for the answer, so this suits fast chains; for slower ones answer `{}` at once and send later with `POST /v1/conversations/{conversation_id}/messages`. `fallback: "auto"` sends the markdown as plain text where a channel has no formatting, and the reply goes out as one message, so keep it within the channel's text limit (4096 characters on Telegram). See [Events and webhooks](/concepts/events-and-webhooks#reply-in-the-webhook-response).
* Keep chat history per `conversation.id`, or read it from `GET /v1/conversations/{conversation_id}/messages`.

## Full example

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


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