Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Create a cart and add a line item -> (Invalid global id `6666256056405`)

Create a cart and add a line item -> (Invalid global id `6666256056405`)

robertbrower
Tourist
5 0 1

Hello. I am creating a product, it's metafield, and image using the Admin API. I want to return a URL to the cart with the product selected and a quantity of 1.

 

This is the response of the POST Product request /admin/api/2021-10/products.json:

 

{
    "product": {
        "id"6666256056405,
        "title""Burton Custom Freestyle 151",
        "body_html""<strong>Good snowboard!</strong>",
        "vendor""Burton",
        "product_type""Snowboard",
        "created_at""2021-12-15T08:02:38+01:00",
        "handle""burton-custom-freestyle-151",
        "updated_at""2021-12-15T08:02:40+01:00",
        "published_at""2021-12-15T08:02:38+01:00",
        "template_suffix"null,
        "status""active",
        "published_scope""web",
        "tags""Barnes & Noble, Big Air, John's Fav",
        "admin_graphql_api_id""gid://shopify/Product/6666256056405",
        "variants": [
            {
                "id"39672718229589,
                "product_id"6666256056405,
                "title""Default Title",
                "price""0.00",
                "sku""",
                "position"1,
                "inventory_policy""deny",
                "compare_at_price"null,
                "fulfillment_service""manual",
                "inventory_management"null,
                "option1""Default Title",
                "option2"null,
                "option3"null,
                "created_at""2021-12-15T08:02:39+01:00",
                "updated_at""2021-12-15T08:02:39+01:00",
                "taxable"true,
                "barcode"null,
                "grams"0,
                "image_id"null,
                "weight"0.0,
                "weight_unit""kg",
                "inventory_item_id"41768959115349,
                "inventory_quantity"0,
                "old_inventory_quantity"0,
                "requires_shipping"true,
                "admin_graphql_api_id""gid://shopify/ProductVariant/39672718229589"
            }
        ],
        "options": [
            {
                "id"8578240348245,
                "product_id"6666256056405,
                "name""Title",
                "position"1,
                "values": [
                    "Default Title"
                ]
            }
        ],
        "images": [],
        "image"null
    }
}

 

It looks like there is one default variant.

 

Following the example in the documentation here: https://shopify.dev/custom-storefronts/cart#create-a-cart-and-add-a-line-item

 

I receive this error:

 

{
    "errors": [
        {
            "message""Variable $cartInput of type CartInput was provided invalid value for lines.0.merchandiseId (Invalid global id `6666256056405`)",
            "locations": [
                {
                    "line"1,
                    "column"21
                }
            ],
            "extensions": {
                "value": {
                    "lines": [
                        {
                            "quantity"1,
                            "merchandiseId""6666256056405"
                        }
                    ],
                    "attributes": {
                        "key""cart_attribute",
                        "value""This is a cart attribute"
                    }
                },
                "problems": [
                    {
                        "path": [
                            "lines",
                            0,
                            "merchandiseId"
                        ],
                        "explanation""Invalid global id `6666256056405`",
                        "message""Invalid global id `6666256056405`"
                    }
                ]
            }
        }
    ]
}
 
I have searched high and low for how to find the correct global id, and I have tried many different ways I found on the internet but nothing works.
 
Can someone please tell me how to get the global ID from the response from
/admin/api/2021-10/products.json shown above? Thanks.
Replies 3 (3)

Jason
Shopify Partner
11206 226 2315

I don’t have all the context, but it feels like you are creating a product with the REST API, but and then using the storefront API to create carts.

 

The IDs differ between REST and GraphQL. Did you take a look in the docs?

There’s examples of Global IDs (GIDs) in here that should give you some inspiration:

https://shopify.dev/api/admin/getting-started

 

Looking at the reply in REST, you also see the a GID in the data returned (see admin_graphql_api_id property)

 

★ I jump on these forums in my free time to help and share some insights. Not looking to be hired, and not looking for work. http://freakdesign.com.au ★
robertbrower
Tourist
5 0 1

Your asumptions are correct. I guess I should just use the GraphQL interfaces and forget about the REST interfaces

robertbrower
Tourist
5 0 1

I switched to using the admin graphql api and I am able to create the cart and get a working url now! But I had to query the store for the legacy id of the default variant so that I could upload the custom photo of the product as 64bit encoded image because that feature doesn't exist in the graphql api. So the only problem I have left to solve is how do I automatically select my app in the sales channels and apps when I create the product using the admin graphql api?