GuidesE-commerce

Tutorial: read and update a store with the Shopify Admin GraphQL API

By the Banzai Labs teamUpdated on 2 min read

query {products(first: 50) {nodes {titlevariants { sku price }}pageInfo { endCursor }}POSTAdminGraphQLAsk for exactly the fields you need, page by page
A GraphQL query asks Shopify’s Admin API for exactly the fields needed, page by page.

1. The endpoint

const URL = `https://${process.env.SHOP}.myshopify.com/admin/api/${process.env.API_VERSION}/graphql.json`;

async function gql(query: string, variables = {}) {
  const r = await fetch(URL, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json', 'X-Shopify-Access-Token': process.env.SHOPIFY_TOKEN! },
    body: JSON.stringify({ query, variables }),
  });
  const json = await r.json();
  if (json.errors) throw new Error(JSON.stringify(json.errors));
  return json.data;
}

2. Read the catalog, page by page

const Q = `query ($after: String) {
  products(first: 50, after: $after) {
    nodes { id title variants(first: 20) { nodes { id sku price } } }
    pageInfo { hasNextPage endCursor }
  }
}`;

let after = null;
do {
  const { products } = await gql(Q, { after });
  handle(products.nodes);
  after = products.pageInfo.hasNextPage ? products.pageInfo.endCursor : null;
} while (after);

3. Update a price

await gql(`mutation ($product: ID!, $variants: [ProductVariantsBulkInput!]!) {
  productVariantsBulkUpdate(productId: $product, variants: $variants) {
    userErrors { field message }
  }
}`, { product: 'gid://shopify/Product/123', variants: [{ id: 'gid://shopify/ProductVariant/456', price: '49.90' }] });

Always check userErrors: a mutation can return without an HTTP error and still do nothing.

4. Good practices

  • Minimal fieldsEvery requested field has a cost.
  • Watch the costRead the remaining credit in the response.
  • Bulk operationsTo export a whole large catalog.

Comptoir, one of our projects, relies on this API to update prices, stock and product pages across several stores from a conversation with Claude.

Background: launching a Shopify store. An application: translate a Shopify catalog.

Frequently asked questions

How do I get an access token?
By creating an app for the store (depending on your account, from the Shopify admin or the developer dashboard), with only the scopes you need, for example read_products and write_products.
Which API version should I use?
Versions are dated and released every quarter. Pin one in the URL and update it before it is retired.
Why is my request throttled?
Shopify assigns a cost to each query. The response shows the cost and the remaining credit: slow down when it drops, or switch to bulk operations.

Projects where we did it

Read next

All guides