Why GraphQL over REST?
Shopify's GraphQL Admin API lets you request exactly the fields you need in a single round-trip — no over-fetching, no multiple REST calls. It also gives access to newer capabilities like metafield definitions that don't exist in REST.
Authentication
All Admin API calls require an access token in the X-Shopify-Access-Token header:
const SHOPIFY_DOMAIN = "yourstore.myshopify.com";
const ACCESS_TOKEN = process.env.SHOPIFY_ACCESS_TOKEN;
async function shopifyQuery(query, variables = {}) {
const res = await fetch(
`https://${SHOPIFY_DOMAIN}/admin/api/2024-07/graphql.json`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Shopify-Access-Token": ACCESS_TOKEN,
},
body: JSON.stringify({ query, variables }),
}
);
return res.json();
}Fetching the last 10 orders
query GetOrders {
orders(first: 10, sortKey: CREATED_AT, reverse: true) {
edges {
node {
id
name
displayFinancialStatus
totalPriceSet { shopMoney { amount currencyCode } }
customer { firstName lastName email }
}
}
}
}Reading a product metafield
query GetProductMeta($id: ID!) {
product(id: $id) {
title
metafield(namespace: "custom", key: "care_instructions") {
value
type
}
}
}Writing a metafield
mutation SetMeta($input: [MetafieldsSetInput!]!) {
metafieldsSet(metafields: $input) {
metafields { id key value }
userErrors { field message }
}
}Variables:
{
"input": [{
"ownerId": "gid://shopify/Product/123456",
"namespace": "custom",
"key": "care_instructions",
"value": "Machine wash cold",
"type": "single_line_text_field"
}]
}