# How to create a checkout session (https://developer.godaddy.com/en/docs/api-users/commerce/accept-payments/online-payments/create-session)

---
title: How to create a checkout session
description: >-
  Create the server-side checkout session that embedded and hosted checkout both
  start from, and return it to your page.
keywords: >-
  create checkout session, createCheckoutSession, @godaddy/react/server,
  checkout session route, lineItems, draftOrderId, session limits, checkout
  errors, Unified Checkout, GoDaddy Payments
agentNotes:
  permissions:
    - GoDaddy Payments account on the business that owns the store
  scopes:
    - 'commerce.checkout-session:write'
    - 'commerce.transaction:read'
  idempotent: false
  destructive: false
  failureRecovery: >-
    A failure creating the session means your token, its scope, the storeId, the
    order, or the return URLs are wrong, and no money has moved. Session
    creation isn't idempotent, so calling it again creates another session and
    another draft order. Reuse the session you already hold.
related:
  guides:
    - title: How to use hosted checkout
      href: /docs/api-users/commerce/accept-payments/online-payments/hosted
    - title: How to render embedded checkout
      href: /docs/api-users/commerce/accept-payments/online-payments/embedded
    - title: How to configure checkout
      href: /docs/api-users/commerce/accept-payments/online-payments/configure
    - title: How to get set up to accept payments
      href: /docs/api-users/commerce/accept-payments/before-you-start
  concepts:
    - title: About online payments
      href: /docs/api-users/commerce/accept-payments/online-payments
    - title: Checkout session reference
      href: /docs/api-users/commerce/accept-payments/online-payments/session-fields
pageId: 78b989e2-ada7-4aa9-98d8-051988948e96
---

## Overview

Embedded and hosted checkout both start from a checkout session that your server creates. Your server calls `createCheckoutSession` with your Personal Access Token (PAT), then returns the session to your page.

The package has a server half and a browser half. Import `createCheckoutSession` from `@godaddy/react/server` (importing from `@godaddy/react` fails). Your PAT never crosses into the browser.

Session create has no idempotency header. A repeated call creates another session and another draft order rather than returning the first, so guard against double submission in your own code.

## Prerequisites

The following prerequisites are required before you can create a checkout session:

* a GoDaddy Payments account with an active store, and your `storeId`
* the `@godaddy/react` package installed in your server project
* a [Personal Access Token (PAT)](https://developer.godaddy.com/docs/api-users/auth/how-to) with the `commerce.checkout-session:write` scope, plus `commerce.transaction:read` to confirm payments, created on the account that owns the store
* your `channelId`, only when you want a channel other than the store's default

  Go to [How to list sales channels](https://developer.godaddy.com/docs/api-users/commerce/set-up-a-store/channel) to list the channels on your store. That call needs `commerce.channel:read` as well, which checkout itself doesn't use.
* an order, supplied as line items or a draft order ID
* public HTTPS return and success URLs

Go to [How to get set up to accept payments](https://developer.godaddy.com/docs/api-users/commerce/accept-payments/before-you-start) for the account, store, and token.

A Personal Access Token authenticates as the account that created it, not as one store. Scopes limit what a leaked token can do, but not which store it can reach: a token that works for one store works for every store the account owns.

* Create a token that carries only `commerce.checkout-session:write` and `commerce.transaction:read`, rather than reusing a broad one. The second scope lets your server confirm payments.
* Keep it on your server and out of source control.
* The browser authenticates with the session token, so it never needs yours. Don't return it alongside the session.
* If someone else is building the integration for you, create the PAT on your account and share it securely.
* If it leaks, rotate it and treat that as an account-level incident.

Go to [Where a PAT fits](https://developer.godaddy.com/docs/api-users/auth#where-a-pat-fits) for the boundaries a Personal Access Token carries.

## Supply the order

* Choose the form that supplies the order — `lineItems` or `draftOrderId`, not both:

  | Form                            | Use it when                                                                                                                                          |
  | ------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
  | `lineItems` with `lineItemData` | Your own system owns the cart, or you charge an amount with no catalog item behind it. GoDaddy creates the draft order and returns it on the session |
  | `lineItems` with `skuId`        | The product is already in your GoDaddy catalog                                                                                                       |
  | `draftOrderId`                  | You created the order earlier and want to charge it                                                                                                  |

  `draftOrderId` is the only way to know the order ID before the session exists, which matters when the ID has to be part of `successUrl`.

  Go to [Checkout session reference](https://developer.godaddy.com/docs/api-users/commerce/accept-payments/online-payments/session-fields#ways-to-supply-the-order) for the shape of each form.

## Create a checkout session on your server

Add a server route that creates the session, for example `POST /api/checkout-session`. The following procedure creates a session and returns it to the browser.

Session create isn't a read. Exposing it as a GET lets browsers and proxies cache or prefetch it, and every prefetch creates a new session and draft order.

1. Build the session input:

   ```typescript
   const storeId = process.env.STORE_ID;
   if (!storeId) {
     throw new Error('Set STORE_ID');
   }

   const input = {
     storeId,
     returnUrl: 'https://your-app.example.com/cancel',
     successUrl: 'https://your-app.example.com/success',
     lineItems: [
       {
         quantity: 1,
         lineItemData: {
           name: 'Product name',
           priceData: { currencyCode: 'USD', unitAmount: 1099 },
         },
       },
     ],
     paymentMethods: {
       card: { processor: 'godaddy', checkoutTypes: ['standard'] },
     },
   };
   ```

Pass your PAT as `personalAccessToken` inside `auth`. The token must carry `commerce.checkout-session:write`. Without it the call has no credential to authorize with.

2. Create the session:

   ```typescript
   import { createCheckoutSession } from '@godaddy/react/server';

   const personalAccessToken = process.env.GODADDY_PAT;
   if (!personalAccessToken) {
     throw new Error('Set GODADDY_PAT');
   }

   const session = await createCheckoutSession(input, {
     auth: { personalAccessToken },
   });
   if (!session?.url) {
     throw new Error('Checkout returned no session');
   }
   ```

3. Return the session to the browser:

   ```typescript tab="Embedded"
   return Response.json(session);
   ```

   ```typescript tab="Hosted"
   return Response.json({ url: session.url });
   ```

Embedded checkout needs the whole session object including its `token`. The checkout component exchanges that token for a JWT, so stripping it stops checkout loading. Hosted checkout needs only `url`, because no component in the browser uses the session token. Your PAT isn't part of the session.

## Limits

The following table lists the limits session create enforces:

| Limit                        | Value                                                                                                                                |
| ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------ |
| Line items per session       | 20                                                                                                                                   |
| Currencies per session       | One. Every line item uses the same currency                                                                                          |
| Amounts                      | `unitAmount` in the currency's minor unit, so `1099` is $10.99                                                                       |
| Required fields              | `storeId`, `returnUrl`, `successUrl`, and an order                                                                                   |
| Return URLs                  | Public HTTPS. Checkout rejects loopback URLs like `localhost` and `127.0.0.1` at session create, so local development needs a tunnel |
| Placeholders in `successUrl` | Not expanded. Checkout stores the URL exactly as you sent it                                                                         |

## Common errors

Session create validates its input before it reaches Checkout. The following table lists the messages it returns and what to change.

| Error message                                                      | What to change                                                          |
| ------------------------------------------------------------------ | ----------------------------------------------------------------------- |
| `Either draftOrderId or lineItems is required`                     | Supply an order one way or the other                                    |
| `Provide either draftOrderId or lineItems, not both`               | Remove whichever one you don't need                                     |
| `channelId is required when creating a draft order from lineItems` | Send `channelId` explicitly. The store's default channel didn't resolve |
| `At least one line item is required`                               | `lineItems` can't be empty                                              |
| `Maximum 20 line items allowed per checkout session`               | Reduce the order to 20 line items or fewer                              |
| `Provide exactly one of skuId or lineItemData`                     | Each line item references a SKU or defines its own data, not both       |
| `All lineItemData line items must use the same currencyCode`       | Use one currency across the order                                       |

A failure here means no session exists and no money has moved. A failure after a valid session exists is a payment failure, which the checkout interface reports to the shopper.
