# Checkouts (/api/checkouts)

<Callout type="success" title="Live">
  Checkouts work today, with test and live keys. Every Xtopay pay page uses
  the same checkout: your customer sees the same thing either way.
</Callout>

<Callout type="info" title="MoMo only for now">
  Card payments are coming soon. For now a checkout offers Mobile Money
  only: `methods` with only `card` is refused, and `card` in a list is left
  out.
</Callout>


A checkout is what's to be paid: an amount, what it's for and, if you know
them, who's paying. Your customer opens its `url` (`pay.xtopay.co/<id>`) and
chooses how to pay: **Mobile Money** (a PIN prompt on their phone) or a
**bank card** (on our payment partner's secure page). When it's paid, they
go back to your `callback_url`.

## Create a checkout [#create-a-checkout]

<Endpoint method="POST" path="/v1/checkouts" />

<TypeTable
  type={{
    amount: {
      type: "number",
      description: "What to charge, in cedis. At least 1.",
      required: true,
    },
    description: {
      type: "string",
      description: "What it's for, shown to your customer. Up to 200 characters.",
      required: true,
    },
    reference: {
      type: "string",
      description:
        "Your own ID for it, like an order number. Each checkout's reference must be new, so retrying can't make two.",
    },
    callback_url: {
      type: "string",
      description:
        "Where your customer goes once it's paid, or if they go back. HTTPS. Xtopay adds the checkout's ID and your reference to it.",
    },
    customer: {
      type: "object",
      description:
        "name, email and phone, if you know them. The phone fills in their MoMo number; they can change it.",
    },
    methods: {
      type: "string[]",
      description:
        "Which ways to pay to offer: mtn, telecel, at, card. Default: those on in your Checkout settings.",
    },
    fees: {
      type: "string",
      description:
        "Who pays Xtopay's fee: customer (added to what they pay; the checkout shows it) or business (out of amount). Default: your Checkout settings.",
    },
    metadata: {
      type: "object",
      description:
        "Up to 20 keys of your own, with string values. Sent back on the payment and in webhooks.",
    },
  }}
/>

```bash tab="cURL"
curl https://api.xtopay.co/v1/checkouts \
  -H "Authorization: Bearer sk_test_…" \
  -H "Content-Type: application/json" \
  -d '{
    "amount": 350,
    "description": "Jollof party pack",
    "reference": "order_1042",
    "callback_url": "https://yourshop.com/thanks"
  }'
```

```js tab="Node.js"
const res = await fetch("https://api.xtopay.co/v1/checkouts", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.XTOPAY_SECRET_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    amount: 350,
    description: "Jollof party pack",
    reference: "order_1042",
    callback_url: "https://yourshop.com/thanks",
  }),
});
const { success, data, message } = await res.json();
if (!success) throw new Error(message);
// Send your customer to data.url
```

```python tab="Python"
import os
import requests

res = requests.post(
    "https://api.xtopay.co/v1/checkouts",
    headers={"Authorization": f"Bearer {os.environ['XTOPAY_SECRET_KEY']}"},
    json={
        "amount": 350,
        "description": "Jollof party pack",
        "reference": "order_1042",
        "callback_url": "https://yourshop.com/thanks",
    },
)
body = res.json()
if not body["success"]:
    raise Exception(body["message"])
checkout_url = body["data"]["url"]
```

```php tab="PHP"
$ch = curl_init("https://api.xtopay.co/v1/checkouts");
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        "Authorization: Bearer " . getenv("XTOPAY_SECRET_KEY"),
        "Content-Type: application/json",
    ],
    CURLOPT_POSTFIELDS => json_encode([
        "amount" => 350,
        "description" => "Jollof party pack",
        "reference" => "order_1042",
        "callback_url" => "https://yourshop.com/thanks",
    ]),
]);
$body = json_decode(curl_exec($ch), true);
if (!$body["success"]) throw new Exception($body["message"]);
header("Location: " . $body["data"]["url"]);
```

It answers with the checkout:

```json title="201 Created"
{
  "success": true,
  "data": {
    "id": "chk_8f3k2m9q4z7c1v5xw0bn",
    "url": "https://pay.xtopay.co/chk_8f3k2m9q4z7c1v5xw0bn",
    "mode": "test",
    "amount": 350,
    "currency": "GHS",
    "description": "Jollof party pack",
    "reference": "order_1042",
    "status": "pending",
    "payment": null,
    "customer": { "name": null, "email": null, "phone": null },
    "methods": null,
    "callback_url": "https://yourshop.com/thanks",
    "metadata": {},
    "expires_at": "2026-10-26T09:15:00Z",
    "created_at": "2026-10-25T09:15:00Z"
  }
}
```

Send your customer to `url`. It works on phones and computers; there's
nothing to embed. A reference used before gets `409 taken`.

## The checkout's status [#the-checkouts-status]

| Status | Meaning |
| --- | --- |
| `pending` | Waiting for your customer to pay |
| `paid` | Paid. `payment` is the payment's reference (`XTP-…`) |
| `expired` | Not paid within 24 hours. Create a new one if they still want to pay |

A customer can try more than once on the same checkout, say if their first
MoMo prompt timed out, or to switch to a card. While a try is still waiting
they can't start another for a couple of minutes, and once one succeeds the
checkout is paid and takes no more.

## When your customer comes back [#when-your-customer-comes-back]

Once it's paid, they go to your `callback_url` with the checkout's ID and
your reference:

```text
https://yourshop.com/thanks?checkout=chk_8f3k2m9q4z7c1v5xw0bn&reference=order_1042
```

If they paid by card on our partner's page, they see their receipt first,
with a button back to you. **Back** on the checkout also goes to your
`callback_url`.

<Callout type="warn" title="Check before you fulfil">
  Anyone can open that URL. Before you hand anything over, get the checkout
  (or wait for the [payment.successful webhook](/api/webhooks)) and check
  that its status is `paid` and its amount is what you expect.
</Callout>

Its payment is an ordinary [payment](/api/payments), with `checkout` set to
the checkout's ID, `source.type` `checkout`, and your `metadata`.

## Get a checkout [#get-a-checkout]

<Endpoint method="GET" path="/v1/checkouts/{id}" />

```bash
curl https://api.xtopay.co/v1/checkouts/chk_8f3k2m9q4z7c1v5xw0bn \
  -H "Authorization: Bearer sk_test_…"
```

## List checkouts, or find one by your reference [#list-checkouts-or-find-one-by-your-reference]

<Endpoint method="GET" path="/v1/checkouts" />

Newest first: `reference`, `status`, `page` and `page_size` (up to 100).
With `reference`, it's at most one: useful after a retry gets `409 taken`.

```bash
curl "https://api.xtopay.co/v1/checkouts?reference=order_1042" \
  -H "Authorization: Bearer sk_test_…"
```

## In test mode [#in-test-mode]

A test key's checkouts say **Test mode**, and both ways to pay go to the
test provider's page, where you approve or decline the payment yourself.
See [Testing](/api/testing#test-checkouts).
