Tutorial · AI agents · ~15 minutes

Build a "reorder my usual" agent tool

A great first agent commerce feature: the user says "order my usual" and the agent places a real order — once, even if it retries. Here's how to build that tool on OrderCore, from a catalog SKU to a completed idempotent order.

What you'll build

A single tool your agent can call — reorder(sku_id, quantity) — that creates a checkout session for the item and completes it into an order. Because completion is idempotent, a retried "order my usual" never creates a duplicate.

Prerequisites

Step 1 — the reorder function

This is the whole tool. It mints a checkout token, creates a session for the SKU, and completes it:

const BASE = 'https://api.ordercore.ai';

async function reorder(skuId, quantity = 1) {
  const { token } = await (await fetch(BASE + '/v1/account/checkout-access-tokens', {
    method: 'POST', headers: { 'X-API-Key': process.env.ORDERCORE_API_KEY } })).json();
  const H = { 'Content-Type': 'application/json', 'X-Checkout-Token': token };

  const { session_id } = await (await fetch(BASE + '/ucp/public/checkout/sessions', {
    method: 'POST', headers: H,
    body: JSON.stringify({ line_items: [{ item: { id: skuId }, quantity }], currency: 'USD' }) })).json();

  const order = await (await fetch(BASE + `/ucp/public/checkout/sessions/${session_id}/complete`, {
    method: 'POST', headers: H,
    body: JSON.stringify({ session_id, payment_data: { handler_id: 'card', payment_intent_id: 'pi_...' } }) })).json();

  return order;   // same session -> same order, even if the agent retries
}

Step 2 — expose it to the agent

Describe the tool so the model knows when to call it. The shape is the same across providers; here's the idea:

{
  name: "reorder",
  description: "Reorder a previously purchased item by SKU. Safe to retry.",
  parameters: { type: "object",
    properties: { sku_id: { type: "string" }, quantity: { type: "integer", minimum: 1 } },
    required: ["sku_id"] }
}

Prefer not to hand-write schemas? Point Claude/GPT at the MCP server, or pull ready schemas from /direct-ai/tooling.

Step 3 — why the retry is safe

If the agent calls reorder twice for the same session (timeout, user impatience, framework retry), OrderCore returns the same order — one order, one charge. That's the idempotency guarantee you'd otherwise build yourself.

Run it without a key

The demo runs this exact create → complete → retry flow offline in mock mode and proves the order matches:

Run the demo → Full quickstart (JS + curl)

Related