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.

Fetch checkout via storefront API with checkout id from checkout creation webhook

Fetch checkout via storefront API with checkout id from checkout creation webhook

sakuraischerry
Tourist
8 0 3

How can I create checkout id by checkout creation webhook? I'd like to create checkout id like this: gid://shopify/Checkout/foo?key=bar to fetch checkout via storefront API.

 

JSON from checkout creation webhook is below:

 

{
    "id": 12345,
    "token": "token",
    "cart_token": "cart_token",
    "email": null,
    "gateway": null,
    "buyer_accepts_marketing": true,
    "created_at": "2019-11-26T12:47:46-08:00",
    "updated_at": "2019-11-26T12:47:46-08:00",
    "landing_site": "/",
    "note": null,
    "note_attributes": [],
    "referring_site": "",
    "shipping_lines": [],
    "taxes_included": false,
    "total_weight": 500,
    "currency": "USD",
    "completed_at": null,
    "closed_at": null,
    "user_id": null,
    "location_id": null,
    "source_identifier": null,
    "source_url": null,
    "device_id": null,
    "phone": null,
    "customer_locale": "en",
    "line_items": [
        {
            "applied_discounts": [],
            "key": "key",
            "destination_location_id": null,
            "fulfillment_service": "manual",
            "gift_card": false,
            "grams": 500,
            "origin_location_id": 1234,
            "product_id": 1234,
            "properties": [],
            "quantity": 1,
            "requires_shipping": true,
            "sku": "SKU",
            "tax_lines": [],
            "taxable": false,
            "title": "Title",
            "variant_id": 1234,
            "variant_title": "",
            "variant_price": "16.00",
            "vendor": "Vendor",
            "user_id": null,
            "unit_price_measurement": {
                "measured_type": null,
                "quantity_value": null,
                "quantity_unit": null,
                "reference_value": null,
                "reference_unit": null
            },
            "country_hs_codes": [],
            "country_code_of_origin": null,
            "province_code_of_origin": null,
            "harmonized_system_code": null,
            "compare_at_price": null,
            "line_price": "16.00",
            "price": "16.00",
            "presentment_title": "Title"
        }
    ],
    "name": "#12345",
    "source": null,
    "abandoned_checkout_url": "https://mystore.com/12345/checkouts/foo/recover?key=bar",
    "discount_codes": [],
    "tax_lines": [],
    "source_name": "web",
    "presentment_currency": "USD",
    "total_discounts": "0.00",
    "total_line_items_price": "16.00",
    "total_price": "16.00",
    "total_tax": "0.00",
    "subtotal_price": "16.00"
}

I tried to fetch checkout with id in the JSON, but I couldn't get checkout.

Replies 12 (12)

zubairmohsin
Shopify Partner
59 1 11

According to docs at below link:

 

https://help.shopify.com/en/api/storefront-api/reference/scalar/#id-2019-10

For storefront-api, IDs don't work like "gid://shopify/Product/10079785100". 

 

P.S. Webhooks data adhere to Shopify Admin API data format.

sakuraischerry
Tourist
8 0 3

For storefront-api, IDs don't work like "gid://shopify/Product/10079785100".

Yeah, I know we should make ID base64 format for storefront API.

zubairmohsin
Shopify Partner
59 1 11

You are right... if you have "gid" you can base64 encode it and get id for storefront-api.

 

In order to have a "gid" for Checkout, it should be on GraphQL Admin API.

 

Referring to GraphQL Admin API docs, I didn't find "Checkout" in Queryable objects section. 

https://help.shopify.com/en/api/graphql-admin-api/reference/object 

sakuraischerry
Tourist
8 0 3

When I created checkout via storefront API refer to checkoutcreate , I got checkout id like this: Z2lkOi8vc2hvcGlmeS9DaGVja291dC9mb28/a2V5PWJhcg== which means gid://shopify/Checkout/foo?key=bar. If I can create checkout id like that, I can use the id for storefront API. Therefore, I'd like to know if I can create the id by JSON of checkout creation webhook. Especially I want to know what foo and bar are.

zubairmohsin
Shopify Partner
59 1 11

Not sure about this one... What's your end goal wit this? you have the "checkout id" from storefront-api, do you want it for later use?? If yes, store it in database while you are creating the checkout... it could be one way to do it?

sakuraischerry
Tourist
8 0 3

The goal is to get checkout id for storefront API by JSON of checkout creation webhook. I will use the checkout id for checkoutDiscountCodeApplyV2

zubairmohsin
Shopify Partner
59 1 11

I am not sure if its possible to via checkout/create webhook's json data. 

 

Since you are receiving webhooks, I am assuming you have an app and a server and most probably database. So one workaround would be to store the checkout id while creating the checkout in database and get it whenever you want for later use. 

 

Let's wait for someone else's input too on this one. 

 

Cheers. 

rishi1897
Shopify Partner
53 3 5

how did you do it assuming it had been implemented by now

??

Tetris
Visitor
1 0 1

From the webhook you can get abandoned_checkout_url
abandoned_checkout_url=https://????.com/11111/checkouts/xxxx/recover?key=yyyy
xxxx is the token of checkout

The checkout ID using Storefront API is
base64_encode("gid://shopify/Checkout/xxxx?key=yyyy");

dkjonzy2
Shopify Partner
3 0 0

This solution is not working. The Checkout query still return "node": null.

 

Here is the query I am using

query {
node(id: "Z2lkOi8vc2hvcGlmeS9DaGVja291dC9iYTMzNTk0YmI2ZDM1NTQ5NWY2YTIyMTQwN2ZlMGRlND9rZXk9ZDI3YTgzNmQxYjg1ZDQ4OTdjZWEzNDgwZWQ1ZDQwZDc="){
id ... on Checkout {
id
email
webUrl
}
}
}

rishi1897
Shopify Partner
53 3 5

same issue, any luck going forward?

Francismori7
Shopify Partner
1 0 1

Hey Rishi,

I noticed that when I used the GraphiQL app on my store, it wouldn't work fetching the checkout.

Then, I realised that storefronts are segmented; the GraphiQL app has a storefront API token that can manage its own checkouts, and my other app had a different storefront API token that manages its own checkouts as well.

 

The other one is UNABLE TO access the checkouts created by the other token. 

 

Hope this helps