Introduction
Thank you for reading this post, don't forget to subscribe!Shopify provides powerful APIs that allow developers to extend the platform’s functionality and create custom solutions tailored to specific business needs. The two main APIs, Admin API and Storefront API, offer extensive capabilities for managing store data and creating unique shopping experiences. This comprehensive guide will help you master both the Admin API and Storefront API, providing detailed insights, use cases, and practical examples.
The Admin API allows you to manage store data and perform administrative tasks. It provides access to a wide range of resources, including products, orders, customers, inventory, and more. The Admin API is ideal for backend applications that need to interact with a store’s data.
The Storefront API is designed for building custom storefronts and enhancing the customer shopping experience. It allows you to retrieve product data, create checkout processes, and interact with customers without needing to access the Shopify Admin. The Storefront API is perfect for building headless commerce solutions, mobile apps, and personalized shopping experiences.
Prerequisites:
Tools Needed:
Step 1: Create a Shopify App
Step 2: Configure API Permissions
Step 3: Authenticate with the Admin API
Use the API key and secret to authenticate requests. The Admin API supports both REST and GraphQL.
Example: Authenticating REST API with Node.js
javascript
const axios = require('axios');
const shop = ‘your-store.myshopify.com’;
const apiKey = ‘your-api-key’;
const apiPassword = ‘your-api-password’;
const getProducts = async () => {
try {
const response = await axios.get(`https://${apiKey}:${apiPassword}@${shop}/admin/api/2022-01/products.json`);
console.log(response.data);
} catch (error) {
console.error(error);
}
};
getProducts();
Example: Authenticating GraphQL API with Node.js
javascript
const fetch = require('node-fetch');
const shop = ‘your-store.myshopify.com’;
const accessToken = ‘your-access-token’;
const query = `
{
products(first: 10) {
edges {
node {
id
title
}
}
}
}
`;
const fetchProducts = async () => {
try {
const response = await fetch(`https://${shop}/admin/api/2022-01/graphql.json`, {
method: ‘POST’,
headers: {
‘Content-Type’: ‘application/json’,
‘X-Shopify-Access-Token’: accessToken,
},
body: JSON.stringify({ query }),
});
const data = await response.json();
console.log(data);
} catch (error) {
console.error(error);
}
};
fetchProducts();
Managing Products
javascript
const createProduct = async () => {
const product = {
product: {
title: "Custom Product",
body_html: "<strong>Great product!</strong>",
vendor: "Your Vendor",
product_type: "Custom",
}
};
try {
const response = await axios.post(`https://${apiKey}:${apiPassword}@${shop}/admin/api/2022-01/products.json`, product);
console.log(response.data);
} catch (error) {
console.error(error);
}
};
createProduct();
javascript
const updateProduct = async (productId) => {
const product = {
product: {
id: productId,
title: "Updated Product Title",
}
};
try {
const response = await axios.put(`https://${apiKey}:${apiPassword}@${shop}/admin/api/2022-01/products/${productId}.json`, product);
console.log(response.data);
} catch (error) {
console.error(error);
}
};
updateProduct(‘your-product-id’);
javascript
const deleteProduct = async (productId) => {
try {
const response = await axios.delete(`https://${apiKey}:${apiPassword}@${shop}/admin/api/2022-01/products/${productId}.json`);
console.log(response.data);
} catch (error) {
console.error(error);
}
};
deleteProduct(‘your-product-id’);
Managing Orders
javascript
const getOrders = async () => {
try {
const response = await axios.get(`https://${apiKey}:${apiPassword}@${shop}/admin/api/2022-01/orders.json`);
console.log(response.data);
} catch (error) {
console.error(error);
}
};
getOrders();
javascript
const createOrder = async () => {
const order = {
order: {
line_items: [
{
variant_id: 447654529,
quantity: 1
}
]
}
};
try {
const response = await axios.post(`https://${apiKey}:${apiPassword}@${shop}/admin/api/2022-01/orders.json`, order);
console.log(response.data);
} catch (error) {
console.error(error);
}
};
createOrder();
Step 1: Create a Storefront Access Token
Step 2: Querying the Storefront API
Use the Storefront API to query product data and create custom storefronts. The Storefront API uses GraphQL.
Example: Fetching Products
javascript
const fetch = require('node-fetch');
const shop = ‘your-store.myshopify.com’;
const storefrontAccessToken = ‘your-storefront-access-token’;
const query = `
{
products(first: 10) {
edges {
node {
id
title
description
}
}
}
}
`;
const fetchProducts = async () => {
try {
const response = await fetch(`https://${shop}/api/2022-01/graphql`, {
method: ‘POST’,
headers: {
‘Content-Type’: ‘application/json’,
‘X-Shopify-Storefront-Access-Token’: storefrontAccessToken,
},
body: JSON.stringify({ query }),
});
const data = await response.json();
console.log(data);
} catch (error) {
console.error(error);
}
};
fetchProducts();
Creating a Checkout
javascript
const createCheckout = async () => {
const mutation = `
mutation {
checkoutCreate(input: {}) {
checkout {
id
webUrl
}
}
}
`;
try {
const response = await fetch(`https://${shop}/api/2022-01/graphql`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Shopify-Storefront-Access-Token': storefrontAccessToken,
},
body: JSON.stringify({ query: mutation }),
});
const data = await response.json();
console.log(data);
} catch (error) {
console.error(error);
}
};
createCheckout();
javascript
const addLineItems = async (checkoutId, lineItems) => {
const mutation = `
mutation checkoutLineItemsAdd($checkoutId: ID!, $lineItems: [CheckoutLineItemInput!]!) {
checkoutLineItemsAdd(checkoutId: $checkoutId, lineItems: $lineItems) {
checkout {
id
lineItems(first: 5) {
edges {
node {
title
quantity
}
}
}
}
}
}
`;
const variables = {
checkoutId: checkoutId,
lineItems: lineItems,
};
try {
const response = await fetch(`https://${shop}/api/2022-01/graphql`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Shopify-Storefront-Access-Token': storefrontAccessToken,
},
body: JSON.stringify({ query: mutation, variables }),
});
const data = await response.json();
console.log(data);
} catch (error) {
console.error(error);
}
};
addLineItems(‘your-checkout-id’, [{ variantId: ‘variant-id’, quantity: 1 }]);
Mastering Shopify’s Admin API and Storefront API opens up a world of possibilities for creating custom solutions and enhancing the functionality of Shopify stores. By understanding the differences between these APIs and following best practices, you can develop powerful applications that cater to the unique needs of merchants and their customers. Whether you’re building backend integrations or custom storefronts, this guide provides the foundation you need to get started and succeed in Shopify app development. Happy coding!
For More Information: http://www.ecbinternational.com