Hi, I’m having trouble accessing cart ID with Storefront API. This is with a custom private app I need to develop for a client.
const express = require('express');
const cors = require('cors');
const axios = require('axios');
require('dotenv').config();
const app = express();
app.use(cors());
app.use(express.json());
app.post('/checkout-update', async (req, res) => {
try {
const checkout = req.body;
console.log('THIS IS THE CHECKOUT ID:', checkout.id);
const encodedCheckoutId = Buffer.from(`gid://shopify/Checkout/${checkout.id}`).toString('base64');
console.log('Encoded Checkout ID:', encodedCheckoutId);
const graphqlEndpoint = `https://${process.env.SHOP_NAME}.myshopify.com/api/2023-07/graphql.json`;
const headers = {
'X-Shopify-Storefront-Access-Token': process.env.STOREFRONT_ACCESS_TOKEN,
'Content-Type': 'application/json'
};
const checkoutQuery = `
query getCheckout($checkoutId: ID!) {
node(id: $checkoutId) {
... on Checkout {
id
webUrl
lineItems(first: 5) {
edges {
node {
title
quantity
}
}
}
}
}
}
`;
const variables = {
checkoutId: encodedCheckoutId
};
const response = await axios.post(graphqlEndpoint, { query: checkoutQuery, variables }, { headers });
if (response.data && response.data.data) {
console.log('GraphQL Response:', JSON.stringify(response.data.data, null, 2));
} else {
console.log('Unexpected GraphQL response:', JSON.stringify(response.data, null, 2));
}
} catch (error) {
console.error('Internal Server Error:', error);
}
res.status(200).send('GraphQL Test Completed');
});
const port = process.env.PORT || 3001;
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
Give me this response:
0|index | Server running on port 3003
0|index | THIS IS THE CHECKOUT ID: 981820079255243500
0|index | This is the encoded id: Z2lkOi8vc2hvcGlmeS9DaGVja291dC85ODE4MjAwNzkyNTUyNDM1MDA=
0|index | Encoded Checkout ID: Z2lkOi8vc2hvcGlmeS9DaGVja291dC85ODE4MjAwNzkyNTUyNDM1MDA=
0|index | GraphQL Response: {
0|index | "node": null
0|index | }
As you can see I get checkout ID from the webhook but get a null response in my query. Any help would be greatly appreciated.