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:

Rust logo over black marble background.

Handling memory leaks in Rust

Learn how to manage memory leaks in Rust, avoid unsafe behavior, and use tools like weak references to ensure efficient programs.

Ukeje Goodness
Nov 20, 2024 â‹… 4 min read
Robot pretending to be a person.

Using curl-impersonate in Node.js to avoid blocks

Bypass anti-bot measures in Node.js with curl-impersonate. Learn how it mimics browsers to overcome bot detection for web scraping.

Antonello Zanini
Nov 20, 2024 â‹… 13 min read
Solving Eventual Consistency In Frontend

Solving eventual consistency in frontend

Handle frontend data discrepancies with eventual consistency using WebSockets, Docker Compose, and practical code examples.

Kayode Adeniyi
Nov 19, 2024 â‹… 6 min read
How To Use Lazy Initialization Pattern With Rust 1.80

How to use the lazy initialization pattern with Rust 1.80

Efficient initializing is crucial to smooth-running websites. One way to optimize that process is through lazy initialization in Rust 1.80.

Yashodhan Joshi
Nov 18, 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