How to create a checkout session
View as MarkdownCreate the server-side checkout session that embedded and hosted checkout both start from, and return it to your page.
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/reactpackage installed in your server project -
a Personal Access Token (PAT) with the
commerce.checkout-session:writescope, pluscommerce.transaction:readto confirm payments, created on the account that owns the store -
your
channelId, only when you want a channel other than the store's defaultFind your sales channel ID
Go to How to list sales channels to list the channels on your store. That call needs
commerce.channel:readas 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 for the account, store, and token.
Scope the token down and keep it on your server
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:writeandcommerce.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 for the boundaries a Personal Access Token carries.
Supply the order
-
Choose the form that supplies the order —
lineItemsordraftOrderId, not both:Form Use it when lineItemswithlineItemDataYour 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 lineItemswithskuIdThe product is already in your GoDaddy catalog draftOrderIdYou created the order earlier and want to charge it draftOrderIdis the only way to know the order ID before the session exists, which matters when the ID has to be part ofsuccessUrl.Go to Checkout session reference 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.
Use POST, not GET
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.
-
Build the session input:
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'] }, }, };
Session create needs your PAT on every call
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.
-
Create the session:
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'); } -
Return the session to the browser:
return Response.json(session);
Return what the checkout form needs, never your PAT
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.
Last updated on
How is this guide?