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:

debugging javascript web apps

How to master JavaScript debugging for web apps

With the right tools and strategies, JavaScript debugging can become much easier. Explore eight strategies for effective JavaScript debugging, including source maps and other techniques using Chrome DevTools.

Ivy Walobwa
Jan 9, 2025 ⋅ 8 min read
A Deep Dive Into Angular’s FormArray Container

A deep dive into Angular’s FormArray container

This Angular guide demonstrates how to create a pseudo-spreadsheet application with reactive forms using the `FormArray` container.

Kayode Adeniyi
Jan 8, 2025 ⋅ 3 min read
Handling React Loading States With React Loading Skeleton

Handling React loading states with React Loading Skeleton

Implement a loading state, or loading skeleton, in React with and without external dependencies like the React Loading Skeleton package.

Ibadehin Mojeed
Jan 7, 2025 ⋅ 7 min read
Getting Ready For Tailwind V4.0

Getting ready for Tailwind v4.0

The beta version of Tailwind CSS v4.0 was released a few months ago. Explore the new developments and how Tailwind makes the build process faster and simpler.

Oscar Jite-Orimiono
Jan 6, 2025 ⋅ 12 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