2021-08-30
2992
#ecommerce#svelte
Peter Ekene Eze
64146
Aug 30, 2021 â‹… 10 min read

Build an ecommerce site with SvelteKit and the Shopify Storefront API

Peter Ekene Eze Learn, Apply, Share

Recent posts:

Rxjs Adoption Guide: Overview, Examples, And Alternatives

RxJS adoption guide: Overview, examples, and alternatives

Get to know RxJS features, benefits, and more to help you understand what it is, how it works, and why you should use it.

Emmanuel Odioko
Jul 26, 2024 â‹… 13 min read
Decoupling Monoliths Into Microservices With Feature Flags

Decoupling monoliths into microservices with feature flags

Explore how to effectively break down a monolithic application into microservices using feature flags and Flagsmith.

Kayode Adeniyi
Jul 25, 2024 â‹… 10 min read
Lots of multi-colored blue and purplish rectangles.

Animating dialog and popover elements with CSS @starting-style

Native dialog and popover elements have their own well-defined roles in modern-day frontend web development. Dialog elements are known to […]

Rahul Chhodde
Jul 24, 2024 â‹… 10 min read
Using Llama Index To Add Personal Data To Large Language Models

Using LlamaIndex to add personal data to LLMs

LlamaIndex provides tools for ingesting, processing, and implementing complex query workflows that combine data access with LLM prompting.

Ukeje Goodness
Jul 23, 2024 â‹… 5 min read
View all posts

14 Replies to "Build an ecommerce site with SvelteKit and the Shopify Storefront API"

  1. Hi, I am following your steps but I am running into issues. I get the error message below.

    {
    errors: ‘[API] Invalid API key or access token (unrecognized login or wrong password)’
    }
    Cannot read property ‘length’ of undefined

    However, if i use Postman and use the same query, headers, and url i get my test data back so I am not sure what is wrong.

    1. Hi Jarrod,

      Are you still facing this issue?

      If yes, please provide more details.

      At which point are you getting the error and what are the steps to replicate?

      However, It might be worth checking how you’re authorizing the requests to Shopify seeing as the error has to do with Invalid credentials.

      Check if your code is somehow modifying your keys.

      Happy to take a look at it for you if you provide the required details.

      1. Yes, I am still having problems.

        Here is my fetch code:

        try {
        const result = await fetch(import.meta.env.VITE_SHOPIFY_API_ENDPOINT, {
        method: ‘POST’,
        headers: {
        ‘Content-Type’: ‘application/json’,
        ‘X-Shopify-Storefront-Access-Token’: import.meta.env.VITE_SHOPIFY_STOREFRONT_API_TOKEN
        },
        body: JSON.stringify({ query, variables })
        }).then((res) => res.json());

        if (result.errors) {
        console.log({ errors: result.errors });
        } else if (!result || !result.data) {
        console.log({ result });
        return ‘No results found.’;
        }
        console.log(result);
        return result.data;
        } catch (error) {
        console.log(error);
        }

        I have used the storefront API and the Admin API. When I use the Admin API in Postman, I get the proper results back.

  2. Hey Jarrod,
    I had the same problem and it took me a while to figure it out. It’s not pretty but here is how I got it to work.

    import { getProducts } from ‘../store’;
    export async function load(ctx) {
    let products = await getProducts();
    return { props: { products } };
    }

    export let products = [];
    let data = JSON.stringify(products.products.edges);
    let parsedData = JSON.parse(data);
    console.log(“DATA:::”,parsedData);

    Home

    {#each parsedData as product}
    {product.node.title}
    {/each}

    Would be cool to see a better way, but this is what worked for me.

    1. Hey Mike,

      Thanks for your suggestion. I think it got me on the right path but now I have another error.

      So i get ‘cannot read property ‘edges’ of undefined which i assume is because it is empty. If i just check products to see if there is any data ie.

      let data = JSON.stringify(products);

      the browser console says TypeError: Failed to execute ‘fetch’ on ‘Window’: Request cannot be constructed from a URL that includes credentials:

          1. haha, i missed the temp email as it must have expired. I tweeted you the other day b/c couldnt msg you. hopefully we can end this game of tag lol

  3. Hi can tou Share an exemple for this please

    VITE_SHOPIFY_STOREFRONT_API_TOKEN = “ADD_YOUR_API_TOKEN_HERE”
    VITE_SHOPIFY_API_ENDPOINT = “ADD_YOUR_STORE_API_ENDPOINT_HERE”

  4. Great tutorial. I am running into an error when I build the product. I’ve connected this to my own production store and see the following on build. Any help would be appreciated.

    SyntaxError: Unexpected end of JSON input
    at JSON.parse ()
    at Response.json (file:///Users/user/Development/Tutorials/SvelteKit/SvelteKitShopify/sveltekit-shopify-ecommerce/node_modules/@sveltejs/kit/dist/install-fetch.js:546:15)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Module.postToShopify (/src/routes/api/utils/postToShopify.js:6:20)
    at async Module.getProducts (/store.js:14:29)
    at async load (/Users/user/Development/Tutorials/SvelteKit/SvelteKitShopify/sveltekit-shopify-ecommerce/src/routes/index.svelte:14:23)
    at async load_node (file:///Users/user/Development/Tutorials/SvelteKit/SvelteKitShopify/sveltekit-shopify-ecommerce/node_modules/@sveltejs/kit/dist/ssr.js:937:12)
    at async respond$1 (file:///Users/user/Development/Tutorials/SvelteKit/SvelteKitShopify/sveltekit-shopify-ecommerce/node_modules/@sveltejs/kit/dist/ssr.js:1221:15)
    at async render_page (file:///Users/user/Development/Tutorials/SvelteKit/SvelteKitShopify/sveltekit-shopify-ecommerce/node_modules/@sveltejs/kit/dist/ssr.js:1386:19)
    at async resolve (file:///Users/user/Development/Tutorials/SvelteKit/SvelteKitShopify/sveltekit-shopify-ecommerce/node_modules/@sveltejs/kit/dist/ssr.js:1647:10)
    TypeError: Cannot read properties of undefined (reading ‘products’)
    at Module.getProducts (/store.js:59:34)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async load (/Users/user/Development/Tutorials/SvelteKit/SvelteKitShopify/sveltekit-shopify-ecommerce/src/routes/index.svelte:14:23)
    at async load_node (file:///Users/user/Development/Tutorials/SvelteKit/SvelteKitShopify/sveltekit-shopify-ecommerce/node_modules/@sveltejs/kit/dist/ssr.js:937:12)
    at async respond$1 (file:///Users/user/Development/Tutorials/SvelteKit/SvelteKitShopify/sveltekit-shopify-ecommerce/node_modules/@sveltejs/kit/dist/ssr.js:1221:15)
    at async render_page (file:///Users/user/Development/Tutorials/SvelteKit/SvelteKitShopify/sveltekit-shopify-ecommerce/node_modules/@sveltejs/kit/dist/ssr.js:1386:19)
    at async resolve (file:///Users/user/Development/Tutorials/SvelteKit/SvelteKitShopify/sveltekit-shopify-ecommerce/node_modules/@sveltejs/kit/dist/ssr.js:1647:10)
    at async respond (file:///Users/user/Development/Tutorials/SvelteKit/SvelteKitShopify/sveltekit-shopify-ecommerce/node_modules/@sveltejs/kit/dist/ssr.js:1622:10)
    at async Immediate. (file:///Users/user/Development/Tutorials/SvelteKit/SvelteKitShopify/sveltekit-shopify-ecommerce/node_modules/@sveltejs/kit/dist/chunks/index.js:3472:22)
    Cannot read properties of undefined (reading ‘products’)

Leave a Reply