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:

CSS logo in front of pile of green matcha tea, which represents the matcha.css library discussed in this article.

How to style HTML with matcha.css

Matcha, a famous green tea, is known for its stress-reducing benefits. I wouldn’t claim that this tea necessarily inspired the […]

Emmanuel Odioko
Oct 7, 2024 â‹… 10 min read
CSS typography in white on a vibrant red geometric background. Article will focus on the CSS backdrop-filter property and its various functions, including blur, grayscale, brightness, and drop-shadow.

How to use the CSS backdrop-filter property

Backdrop and background have similar meanings, as they both refer to the area behind something. The main difference is that […]

Oscar Jite-Orimiono
Oct 4, 2024 â‹… 10 min read
6 AI Tools For API Testing And Development

6 AI tools for API testing and development

AI tools like IBM API Connect and Postbot can streamline writing and executing API tests and guard against AI hallucinations or other complications.

Frank Joseph
Oct 3, 2024 â‹… 12 min read
Patterns For Efficient DOM Manipulation With Vanilla JavaScript

Patterns for efficient DOM manipulation with vanilla JavaScript

Explore DOM manipulation patterns in JavaScript, such as choosing the right querySelector, caching elements, improving event handling, and more.

Joe Attardi
Oct 2, 2024 â‹… 8 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