Create a cart and go to checkout#

This guide walks through a checkout you build yourself: creating a cart, adding products, setting a shipping address and method, then confirming the cart to receive an order with a checkout URL.

·
Note:

Using the hosted checkout? Then only steps 1 and 2 are yours: create the cart, add the items and send the customer to cart.checkout.url. The hosted checkout asks for the addresses, shipping and payment, and calls confirmCart itself. Don't call confirmCart from your storefront in that case.

Overview#

  1. Create an empty cart with createCart
  2. Add products by SKU with addItemsToCart
  3. Set a shipping address with addShippingAddressToCart
  4. Choose a shipping method with addShippingMethodToCart
  5. Verify the cart is valid, then confirm it with confirmCart
  6. Redirect the customer to the checkout URL

Step 1 — Create a cart#

Input: CartInput!. Every field is optional. With a storefront token the cart belongs to the token's channel; with an API key pass channel_id.

Returns: Cart

NameTypeRequiredDescription
id
ID!
RequiredThe ID of the cart
number
String!
RequiredCart number
total
Money!
RequiredTotal value
subtotal
Money!
RequiredTotal before discounts
mutation CreateCart($input: CartInput!) {
  createCart(input: $input) {
    cart {
      id
      number
      currency
    }
  }
}
{
  "input": {
    "country_code": "NL"
  }
}

Save the returned cart.id — you'll pass it as cart_id in every subsequent mutation.

ℹ
Tip:

Pass country_code to pre-set the delivery country and ensure correct tax calculation from the start. See createCart for all fields.


Step 2 — Add items#

Input: AddItemsToCartInput!

NameTypeRequiredDescription
cart_id
String!
RequiredID of the cart returned in step 1.
items
[ItemInput!]!▾
RequiredItems to add.

Returns: Cart

NameTypeRequiredDescription
id
ID!
RequiredThe ID of the cart
number
String!
RequiredCart number
total
Money!
RequiredTotal value
subtotal
Money!
RequiredTotal before discounts
mutation AddItems($input: AddItemsToCartInput!) {
  addItemsToCart(input: $input) {
    cart {
      id
      items {
        sku
        label
        quantity
        total
      }
      subtotal
      total
    }
  }
}
{
  "input": {
    "cart_id": "72fca344-2a6f-4c3e-b4ca-029920b2522a",
    "items": [
      { "sku": "BLK-HOODIE-M", "quantity": 1 },
      { "sku": "BLK-HOODIE-L", "quantity": 2 }
    ]
  }
}

Step 3 — Set a shipping address#

Input: AddShippingAddressToCartInput!

NameTypeRequiredDescription
cart_id
String!
RequiredID of the cart.
address
AddressInput!▾
RequiredDelivery address.

Returns: Cart

NameTypeRequiredDescription
id
ID!
RequiredThe ID of the cart
number
String!
RequiredCart number
total
Money!
RequiredTotal value
subtotal
Money!
RequiredTotal before discounts
mutation AddShippingAddress($input: AddShippingAddressToCartInput!) {
  addShippingAddressToCart(input: $input) {
    cart {
      id
      delivery {
        address {
          address_line_1
          locality
          postal_code
          country_code
        }
      }
    }
  }
}
{
  "input": {
    "cart_id": "72fca344-2a6f-4c3e-b4ca-029920b2522a",
    "address": {
      "address_line_1": "Keizersgracht 313",
      "locality": "Amsterdam",
      "postal_code": "1016 EE",
      "country_code": "NL"
    }
  }
}

Step 4 — Choose a shipping method#

First, query the available shipping methods for your channel:

query GetShippingMethods($first: Int) {
  shippingMethods(first: $first) {
    methods {
      id
      name
      carrier
    }
  }
}
{ "first": 10 }

Then apply one to the cart:

Input: AddShippingMethodToCartInput!

NameTypeRequiredDescription
cart_id
String!
RequiredID of the cart.
method_id
String!
RequiredID of the shipping method to apply.

Returns: Cart

NameTypeRequiredDescription
id
ID!
RequiredThe ID of the cart
number
String!
RequiredCart number
total
Money!
RequiredTotal value
subtotal
Money!
RequiredTotal before discounts
mutation AddShippingMethod($input: AddShippingMethodToCartInput!) {
  addShippingMethodToCart(input: $input) {
    cart {
      id
      delivery {
        method {
          id
          name
        }
      }
      total
    }
  }
}
{
  "input": {
    "cart_id": "72fca344-2a6f-4c3e-b4ca-029920b2522a",
    "method_id": "e5f6a7b8-c9d0-1234-5678-90abcdef0123"
  }
}

Step 5 — Verify and confirm#

Before confirming, check that the cart has no validation errors:

query CheckCart($id: String!) {
  cart(id: $id) {
    id
    total
    validation_errors {
      code
      message
    }
    checkout {
      url
    }
  }
}
{ "id": "72fca344-2a6f-4c3e-b4ca-029920b2522a" }

Once valid, confirm the cart to convert it into an order:

Input: ConfirmCartInput!

NameTypeRequiredDescription
cart_id
String!
RequiredID of the cart to confirm.

Returns: Order

NameTypeRequiredDescription
id
ID!
RequiredThe ID
number
String!
RequiredOrder number
total
Money!
RequiredTotal value
currency
Currency!
RequiredCurrency code
mutation ConfirmCart($input: ConfirmCartInput!) {
  confirmCart(input: $input) {
    order {
      id
      number
      total
      checkout {
        url
      }
    }
  }
}
{
  "input": {
    "cart_id": "72fca344-2a6f-4c3e-b4ca-029920b2522a"
  }
}

Redirect the customer to order.checkout.url to complete payment.


Notes#

  • confirmCart fails if cart.validation_errors is non-empty — ensure items, shipping address, and shipping method are all set.
  • Money values (total, subtotal) are integers in cents — e.g. 2499 means € 24,99.
  • To add a billing address before confirming, use addBillingAddressToCart with the same AddressInput shape.
  • To add a payment method before confirming, use addPaymentMethodToCart with method_id and the required issuer_id.
  • See Authentication for how to pass your API key.
Query Runnerhttps://afosto.app/graphql

No query loaded

Click play on any code block in the docs to load a query here.