> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ionicfi.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Identify returning customers

> Link each payment to the right customer, read guest buyers' details, and recognize buyers who come back.

A customer (`cus_…`) belongs to one merchant and one mode. The customer a
payment lands on holds that payment's saved card, subscription, and history.

Checkout buyers pay as guests unless a customer is needed. A guest payment has
no customer; the buyer's details stay on the checkout session. Your
integration creates customers for buyers it knows, and checkout creates one
when a subscription needs it or when you ask it to.

## Customers and guests

| Buyer | `creation_source` | Email |
| - | - | - |
| A customer you create with `POST /v1/customers` | `api` unless you set it | Unique among the customers you create, per mode. A duplicate returns `409` with code `CUSTOMER_EMAIL_ALREADY_EXISTS`. |
| A customer created in the dashboard | `dashboard` | Same rule as the API. |
| A customer checkout creates (subscriptions, or `customer_creation: always`) | `checkout` | At most one customer per session. Can match the email of any other customer. |
| A guest | No customer | The email the buyer entered, in the session's `customer_details`. |

Checkout treats the email a buyer types as contact details, not as proof of
identity. It never attaches a payment to an existing customer because the
buyer typed that customer's email.

## How checkout decides

```mermaid theme={null}
flowchart TD
  A[Buyer pays] --> B{Session created with customer?}
  B -- Yes --> C[Payment, saved card, and subscription<br/>attach to that customer]
  B -- No --> D{Subscription, or<br/>customer_creation: always?}
  D -- Yes --> E[Checkout creates a customer for this session<br/>creation_source: checkout]
  D -- No --> F[Buyer pays as a guest<br/>customer: null, details in customer_details]
```

Subscription sessions always require an email, so every subscription signup
has a customer. With `customer_creation: always`, a payment where the buyer
enters no email also completes as a guest.

## Link checkout to a customer you know

When the buyer is signed in to your site, pass their customer ID as
`customer` when you create the session. The payment, any saved card, and any
subscription attach to that customer. Checkout shows the customer's email,
and the buyer can't change it.

```ts TypeScript theme={null}
const session = await ionic.checkout.sessions.create({
  "Idempotency-Key": "order_1002-checkout",
  mode: "payment",
  customer: "cus_2pXqAbc",
  line_items: [
    {
      name: "Pro onboarding",
      amount: 15000,
      currency: "usd",
      quantity: 1,
    },
  ],
  client_reference_id: "order_1002",
  success_url: "https://example.com/success?session_id={CHECKOUT_SESSION_ID}",
  cancel_url: "https://example.com/cart",
});
```

```bash curl theme={null}
curl https://api.ionicfi.com/v1/checkout/sessions \
  -H "Authorization: Bearer $IONIC_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: order_1002-checkout" \
  -d '{
    "mode": "payment",
    "customer": "cus_2pXqAbc",
    "line_items": [
      {
        "name": "Pro onboarding",
        "amount": 15000,
        "currency": "usd",
        "quantity": 1
      }
    ],
    "client_reference_id": "order_1002",
    "success_url": "https://example.com/success?session_id={CHECKOUT_SESSION_ID}",
    "cancel_url": "https://example.com/cart"
  }'
```

* The customer must belong to the same merchant and mode as the session and
  must be active. A deleted customer, or one from another merchant or mode,
  returns `404` with code `CUSTOMER_NOT_FOUND`.
* To show an email for a buyer without a customer record, pass
  `customer_email` instead. Checkout shows it, the buyer can't change it, and
  the payment uses it. Pass either `customer` or `customer_email`; a session
  with both returns `400`.
* Create the customer with `POST /v1/customers` the first time a buyer signs
  up, store its ID on your user record, and pass that ID on every session for
  the same buyer.

## Read a guest's details

A completed guest session has `customer` set to `null`. The buyer's email,
name, phone, and billing address are in `customer_details`, and a shipping
address, when the session collects one, is in `shipping_details`. Read them
from the `checkout.session.completed` event, whose `data.object` is the
session, or retrieve the session after the buyer returns to your
`success_url`.

```ts TypeScript theme={null}
const session = await ionic.checkout.sessions.retrieve({ id: "cs_9dsxIuWOrYmInZK2UJzGj3eZ" });

if (session.customer === null) {
  const guest = {
    order: session.client_reference_id,
    email: session.customer_details?.email ?? null,
    shipTo: session.shipping_details?.address ?? null,
  };
  console.log(guest);
}
```

Each submission replaces the last, so the details reflect what the buyer
entered on the attempt that paid.

## Create a customer for every buyer

Set `customer_creation` to `always` when you want a customer for each buyer
who enters an email. Set it on a payment link to apply it to every checkout
from that link.

In the dashboard, select **Create a customer for every buyer** under
**Payment options** when you create or edit a one-time payment link.

```ts TypeScript theme={null}
const session = await ionic.checkout.sessions.create({
  "Idempotency-Key": "order_1003-checkout",
  mode: "payment",
  customer_creation: "always",
  line_items: [
    {
      name: "Pro onboarding",
      amount: 15000,
      currency: "usd",
      quantity: 1,
    },
  ],
  success_url: "https://example.com/success?session_id={CHECKOUT_SESSION_ID}",
});
```

```bash curl theme={null}
curl https://api.ionicfi.com/v1/checkout/sessions \
  -H "Authorization: Bearer $IONIC_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: order_1003-checkout" \
  -d '{
    "mode": "payment",
    "customer_creation": "always",
    "line_items": [
      { "name": "Pro onboarding", "amount": 15000, "currency": "usd", "quantity": 1 }
    ],
    "success_url": "https://example.com/success?session_id={CHECKOUT_SESSION_ID}"
  }'
```

* The completed session's `customer` holds the customer checkout created, and
  `customer.created` fires with `data.object.creation_source` set to
  `checkout`.
* A session creates at most one customer. When the buyer submits the same
  session again, for example after a declined card, the session keeps that
  customer and updates it with the details from the new submission, and
  `customer.updated` fires when anything changed. A customer you pass as
  `customer` is never changed.
* `customer_creation` applies to payment sessions and one-time payment links.
  Subscription sessions always create a customer, and setting the field on
  them returns `400`.

## Save cards

A saved card belongs to a customer. When you set
`payment_intent_data.setup_future_usage`, checkout offers the buyer the option
to save their card only when the session has a `customer` or
`customer_creation: always`. A guest's card is not saved.

On a one-time payment link in the dashboard, selecting **Save card for future
payments** also selects **Create a customer for every buyer**, so each saved
card has a customer to belong to.

## Keep your records in sync

* **Store customers by `id`.** One email can belong to several customers at
  the same merchant: one you created and one for each session that created a
  customer for that buyer.
* **Find every customer for an email** with the list endpoint. `search`
  matches part of a customer's email, name, or ID, so keep the results whose
  `email` equals the address, ignoring case. Results are paginated: iterate
  with `for await` in the SDK, or pass `next_cursor` as `starting_after` until
  `has_more` is `false`.

```ts TypeScript theme={null}
const email = "jane@example.com";
const customerIds: string[] = [];
for await (const customer of await ionic.customers.list({ search: email, limit: 100 })) {
  if (customer.email.toLowerCase() === email) {
    customerIds.push(customer.id);
  }
}
```

```bash curl theme={null}
curl "https://api.ionicfi.com/v1/customers?search=jane@example.com&limit=100" \
  -H "Authorization: Bearer $IONIC_SECRET_KEY"

# When has_more is true, request the next page with the returned next_cursor:
curl "https://api.ionicfi.com/v1/customers?search=jane@example.com&limit=100&starting_after=cus_2pXqAbc" \
  -H "Authorization: Bearer $IONIC_SECRET_KEY"
```

* **Reuse the customer behind a `409`.** When `POST /v1/customers` returns
  `CUSTOMER_EMAIL_ALREADY_EXISTS`, search for the email and use the customer
  you created earlier. Only customers you create count toward the `409`.

## Guests in the dashboard

The **Guests** tab on the Customers page groups guest payments by email, with
each guest's payment count, spend, and first and last payment. Open a guest to
see their payments. Guest payments in the payments list show the buyer's
email.

## Returning-customer counts

The dashboard counts paying buyers by email within the selected date range,
guests included. A buyer counts once, even if they pay through several
checkouts. They count as new if their first payment falls within that range,
or as returning if their first payment was before it.

## Connected accounts

On a [Connect platform](https://docs.ionicfi.com/guides/connect-customers.md), each connected account has
its own customers and guests, and every rule on this page applies per account.
A checkout session you create for a connected account creates any customer
under that account. Store the merchant ID next to each customer ID, and pass a
`customer` that belongs to the same connected account.

## What's next

> [card] [Manage customers](https://docs.ionicfi.com/guides/customers.md)
>
> Create, update, and list customers, and attach payment methods.

> [card] [Hosted checkout](https://docs.ionicfi.com/guides/checkout.md)
>
> Create a session and send the buyer to the hosted page.


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