DEV Community

Cover image for How I Cut Our Shopify API Usage in Half (Without Losing Features)
Muhammad Masad Ashraf
Muhammad Masad Ashraf

Posted on • Originally published at kolachitech.com

How I Cut Our Shopify API Usage in Half (Without Losing Features)

If you build on Shopify long enough, you hit the wall. Throttled requests. Stalled inventory syncs. Webhook backlogs piling up during a flash sale.

Shopify does not charge you per call in dollars. It charges you in rate limits. Blow through them and your app slows to a crawl. The real cost shows up as engineering time, infrastructure bills, and lost sales from stale data.

Here is what actually moved the needle for us.

First, know what you are paying for

Shopify uses two rate-limiting models depending on the API.

API Model How it works
REST Admin Leaky bucket Fixed requests per second
GraphQL Admin Query cost Points based on complexity
Storefront Query cost Higher limits, complexity-based

The GraphQL model is your friend here. You pay for what you ask for. Ask for less, pay less.

Move from REST to GraphQL

This was our biggest single win. REST makes you stitch together multiple calls to get related data. GraphQL gets it in one.

Fetching an order with its line items, customer, and shipping in REST:

// REST: 4 round trips
const order = await fetch(`/admin/api/2024-01/orders/${id}.json`);
const customer = await fetch(`/admin/api/2024-01/customers/${custId}.json`);
const lineItems = await fetch(`/admin/api/2024-01/orders/${id}/line_items.json`);
const shipping = await fetch(`/admin/api/2024-01/orders/${id}/shipping_address.json`);
Enter fullscreen mode Exit fullscreen mode

The same thing in GraphQL:

# GraphQL: 1 request, only the fields you need
query GetOrder($id: ID!) {
  order(id: $id) {
    name
    customer { firstName lastName email }
    shippingAddress { address1 city zip }
    lineItems(first: 50) {
      edges { node { title quantity } }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

One request. No over-fetching. Lower cost.

The trap: people migrate to GraphQL and then request every field anyway. Don't. Every field adds to your query cost. If you only need a title and price, do not pull variants, images, and metafields.

Stop polling. Use webhooks.

Polling is the silent budget killer. An app that checks for new orders every few seconds burns thousands of calls a day, and most return nothing new.

Webhooks flip it. Shopify pushes the event the moment it happens.

// Instead of polling every 5 seconds...
app.post('/webhooks/orders/create', verifyWebhook, (req, res) => {
  const order = req.body;
  queue.add('process-order', order); // hand off, respond fast
  res.sendStatus(200);
});
Enter fullscreen mode Exit fullscreen mode

Two things to get right:

  1. Respond fast. Acknowledge with a 200 immediately, then process async. Shopify retries if you are slow.
  2. Expect duplicates. Shopify sometimes sends the same event twice. Use an idempotency key so you do not fire redundant follow-up calls.
async function handleEvent(event) {
  const key = event.id;
  if (await seen(key)) return; // already processed
  await markSeen(key);
  await process(event);
}
Enter fullscreen mode Exit fullscreen mode

Use Bulk Operations for big data sets

Paginating through 50,000 products with standard queries shreds your rate limit. The Bulk Operations API runs the whole thing async and hands you one file.

mutation {
  bulkOperationRunQuery(
    query: """
    { products { edges { node { id title } } } }
    """
  ) {
    bulkOperation { id status }
    userErrors { field message }
  }
}
Enter fullscreen mode Exit fullscreen mode
Approach Calls Rate limit hit
Standard pagination Hundreds to thousands High
Bulk Operations One operation Minimal

Poll the operation status, download the JSONL when done, process locally.

Cache the stuff that does not change

A surprising amount of API traffic fetches data that is stable for hours or days. Product descriptions. Collection structure. Store settings.

Data Cache? Why
Product descriptions Yes Rarely changes
Collection structure Yes Stable
Store settings Yes Infrequent changes
Live inventory Carefully Changes constantly
Order status Short TTL Updates often

Invalidate on the relevant webhook instead of guessing at TTLs.

Handle rate limits like an adult

Even a lean app hits limits sometimes. Read the headers Shopify returns and throttle before you crash.

async function callWithBackoff(fn, retries = 5) {
  for (let i = 0; i < retries; i++) {
    const res = await fn();
    if (res.status !== 429) return res;
    const wait = Math.pow(2, i) * 1000; // exponential backoff
    await new Promise(r => setTimeout(r, wait));
  }
  throw new Error('Rate limited after retries');
}
Enter fullscreen mode Exit fullscreen mode

Centralize this in a middleware layer so every call gets the same treatment. Do not scatter retry logic across your codebase.

The checklist

Tactic Impact Effort
REST to GraphQL High Medium
Request only needed fields High Low
Bulk Operations API High Medium
Cache stable data High Medium
Webhooks over polling Very High Medium
Batch requests Medium Low
Smart rate limiting Medium Medium
Deduplicate calls Medium Low
Async processing Medium High
Monitor usage High Low

Start here

If you do nothing else: replace polling with webhooks, switch to GraphQL with trimmed queries, and cache stable data. Those three alone cut our usage roughly in half.

Then layer in batching, async jobs, and monitoring for the long game.

I wrote a longer version with deeper architecture notes on our blog: Reducing Shopify API Consumption Costs. Happy to answer questions in the comments.

What is the worst API throttle situation you have run into? Drop it below.

Top comments (0)