How do I run an iMessage bot on Cloudflare Workers?
Run your bot as a Hono app on Cloudflare Workers and let Keiki handle iMessage: Keiki receives each text, posts a signed turn to your Worker, and delivers whatever your Worker sends back.
How do I run an iMessage bot on Cloudflare Workers?
Run your bot as a Hono app on Cloudflare Workers and let Keiki handle iMessage: Keiki receives each text, posts a signed turn to your Worker, and delivers whatever your Worker sends back. Your Worker verifies the request with @onkeiki/channels, answers 202, and finishes the model call with waitUntil.
Key takeaways
- The model, the prompt and your provider key stay in your Worker. Keiki runs the iMessage line, delivery, turn traces and the team inbox.
- One route,
POST /keiki/turn, does the channel work: verify the signature, acknowledge, run the turn inc.executionCtx.waitUntil. - The open-source imessage-hono-workers-starter repo is a working Worker you can deploy as is.
What does the code look like?
The Hono handler checks the HMAC signature against the raw body and schedules the turn:
const rawBody = await c.req.text()
let request
try {
request = verifyKeikiRequest({ rawBody, headers: c.req.raw.headers }, { signingSecret })
} catch (error) {
if (error instanceof KeikiSignatureError) return c.json({ error: error.code }, 401)
throw error
}
if (request.kind === 'turn') c.executionCtx.waitUntil(runTurn(request.turn, request.turnToken, c.env))
return c.json({ accepted: true }, 202)runTurn calls generateText from the Vercel AI SDK with keikiMessagesPrompt(turn.messages) as the prompt, sends the answer with createTurnClient(...).message(), and settles the turn with completeWithRetry.
How do I connect it to an iMessage line?
Deploy the Worker with npx wrangler deploy or the Deploy to Cloudflare button. In the project folder run npx @onkeiki/cli init --dispatch-url https://your-app.example.com/keiki/turn --skip-install: it signs you in, points an external-harness agent at the Worker and writes the signing secret to .dev.vars (see the CLI docs). Store it and your model key with wrangler secret put, then attach an iMessage line and text it. The external harness docs cover the full turn payload and turn API.
FAQ
Does the bot need a Mac or an Apple ID? No. Keiki runs the iMessage line. Your Worker only needs a public HTTPS route that Keiki can reach.
Why does the Worker respond before the model has answered?
Keiki expects a fast acknowledgment, and a model call with tools can take several seconds. waitUntil keeps the Worker running after the 202, so the reply is sent once it is ready. Cloudflare caps how long that work can run, so long jobs belong in a Queue or Durable Object.
Can the same Worker answer on other channels?
Yes. The turn format is the same on every channel Keiki supports, so attaching another channel to the agent sends those conversations to the same route. Keep tool names about the task, like check_availability, rather than the channel.