Checkout guide

This guide walks you through a complete Shopify checkout transaction using GraphQL: You'll learn how to create a checkout, add line items, and set the shipping rates enabling the order to be completed and shipped to the customer.

Creating the Checkout

A Checkout is a GraphQL type, and you interact with it to pick and pay for items.

To create a Checkout, use the checkoutCreate mutation as follows:

mutation checkoutCreate {
  checkoutCreate(input: {
    lineItems: [{ variantId: "", quantity: 1}]
  }) {
    checkout {
       id
       lineItems {
         title
         quantity
       }
    }
  }
}

Provided the mutation is successful a response like the following is returned:

{
  "data": {
    "checkout": {
       "id": "Z2lkOi8vc2hvcGlmeS9DaGVja291dC8xMjM0",
       "lineItems": [{
         "id": "Z2lkOi8vc2hvcGlmeS9DaGVja291dExpbmVJdGVtLzEyMzQ=",
         "title": "Blue Shirt",
         "Quantity": 1
       }]
    }
  }
}

Adding Items to a Checkout

After the Checkout is created, you will need to add new items using the checkoutLineItemsAdd mutation. As part of this mutation, send the Checkout ID and the line items to be added:

mutation AddLineItems {
  checkoutLineItemsAdd(input: {
    checkoutId: "Z2lkOi8vc2hvcGlmeS9DaGVja291dC8xMjM0",
    lineItems: [{ variantId: "", quantity: 1}]
  }) {
    checkout {
       id
       lineItems {
         title
         quantity
       }
    }
  }
}

In this case, we’ve added a second item of the same variant, returning a quantity of two:

{
  "data": {
    "checkout": {
       "id": "Z2lkOi8vc2hvcGlmeS9DaGVja291dC8xMjM0",
       "lineItems": [{
         "id": "Z2lkOi8vc2hvcGlmeS9DaGVja291dExpbmVJdGVtLzEyMzQ=",
         "title": "Blue Shirt",
         "quantity": 2
       }]
    }
  }
}

Setting a Shipping Address

To get correct shipping rates and to ship the items to the customer, you'll need to set a shipping address. This is done using the checkoutShippingAddressUpdate mutation:

mutation {
  checkoutShippingAddressUpdate(input: {
    checkoutId: "Z2lkOi8vc2hvcGlmeS9DaGVja291dC8xMjM0",
    shippingAddress: {
      lastName: "Doe"
      firstName: "John"
      address1: "123 Test Street"
      province: "QC"
      country: "Canada"
      zip: "H3K0X2"
    }
  }) {
    checkout {
       id
       shippingAddress {
         firstName
         lastName
         address1
         province
         country
         zip
       }
    }
  }
}

If the update is successful, the following is returned:

{
  "data": {
    "checkout": {
       "id": "Z2lkOi8vc2hvcGlmeS9DaGVja291dC8xMjM0",
       "shippingAddress": {
         "lastName": "Doe",
         "firstName": "John",
         "address1": "123 Test Street",
         "province": "QC",
         "country": "Canada",
         "zip": "H3K0X2"
       }
    }
  }
}