Skip to content
Content

How do I take Apple Pay payments inside an iMessage conversation?

Turn on the Payments ability for a Keiki agent and have it call payments_request once the customer agrees to a total.

How do I take Apple Pay payments inside an iMessage conversation?

Turn on the Payments ability for a Keiki agent and have it call payments_request once the customer agrees to a total. On a dedicated Keiki iMessage line, Keiki puts a Pay card in the conversation and the customer pays with Apple Pay in Messages, with no app or web checkout to open.

Key takeaways

  • Your product prices the order. A tool in your app returns line items in US cents, so the model never invents a price or adds numbers.
  • Keiki collects the payment. payments_request runs on Keiki with your organization's connected payment account, and the key never reaches your app or the model.
  • The open-source imessage-apple-pay-checkout repo is a working Next.js and AI SDK example you can fork.

What does the code look like?

Your AI SDK route merges your own tools with the agent's Keiki tools for the turn, which include the payment tools when Payments is on:

export const POST = createKeikiHandler({
  waitUntil,
  onTurn: async ({ instructions, messages, tools, prepareStep, abortSignal }) =>
    streamText({ model, instructions, messages, tools: { ...(await tools()), ...productTools }, stopWhen: stepCountIs(8), prepareStep, abortSignal }),
})

productTools holds quote_order, which returns a payment object with payee_id, description, line_items and goods_type. After the customer says yes, the model passes that object to payments_request unchanged.

How do I set it up?

Run npx @onkeiki/cli init --dispatch-url https://your-app.example.com/api/keiki in the app, turn on Payments for the agent, and connect your payment account through Keiki Code Mode and verify its identity as described in the Payments docs. Create a payee linked to the agent and get its limit approved, set its id as KEIKI_PAYEE_ID, then attach an iMessage line and text it.

FAQ

Does the customer need to install an app to pay? The Pay card opens in Messages and uses Apple Pay on the customer's iPhone, through Keiki Vault. A customer without Keiki Vault, or on a line or channel where no card can be sent, pays through the checkout_url that payments_request returns, and the agent sends that link instead.

How does the agent know the payment went through? When the payment settles, Keiki updates the card and starts a turn with a payment notice. The agent can also check with payments_request_status, and it should not confirm the order before one of them reports it paid.

Can I keep running my own model? Yes. The model loop runs in your app with your provider key through the AI SDK handler. Keiki handles the iMessage line, delivery and the payment request.