Storefront API variantId different than Rest API variantId

I am trying to find the variant Id for my products but when I use the Rest API I get a set of numbers whereas when I use the Storefront API I am getting a long string. Does anyone know the difference and why it exists? Can I get the Storefront API from the Rest API?

1 Like

To help others who find this post I found a solution.

You can use this query to pull the Storefront API variantId given the Rest API variantId

{
productVariant (id: “gid://shopify/ProductVariant/{restVariantId”) {
id
title
storefrontId
}
}

This query needs to be run against this url

https://{API key}:{Password}@{your store}.myshopify.com/admin/api/2019-07/graphql.json

I also would like to note that these headers need to be sent.

“Accept: application/json”,
“Content-Type: application/graphql”

I mention all of this because it was never really explained in the documentation. The basic difference is that the Storefront section in the App you create for your store gives you the credentials for public endpoints on the Storefront API. While private Storefront API endpoints (or admin, hence the /admin in the url) uses the Admin credentials from the App (the API key and password). You also need to make sure that the admin credentials have access to product information.

If any of this information is wrong please feel free to correct me in a reply.

3 Likes

From the Storefront API docs:

https://help.shopify.com/en/api/storefront-api/reference/scalar

ID: Represents a unique identifier, often used to refetch an object or as key for a cache. The ID type appears in a JSON response as a String, but it is not intended to be human-readable. When expected as an input type, any string (such as “4”) or integer (such as 4) input value will be accepted as an ID.> > Admin API example value: “gid://shopify/Product/10079785100”.> > Storefront API example value: “Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0LzEwMDc5Nzg1MTAw”.

If you’re wondering how to convert from one to another:

const storefrontId = "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0LzEwMDc5Nzg1MTAw";
const adminGraphqlId = atob(storefrontId);
console.log(adminGraphqlId) // prints "gid://shopify/Product/10079785100"

Storefront IDs are base64 encoded, and using the javascript atob, you can decode them. Any language has base64 decoding of strings.

If you want to only have the Rest admin ID, you can use this package from Shopify: https://github.com/Shopify/quilt/tree/master/packages/admin-graphql-api-utilities

11 Likes

Thank you very much, you solved my problem!