How to authenticate with the Shopify API

How to authenticate with the Shopify API

I use Node.js. So to programatically connect to Shopify’s Admin API, I use the npm package @shopify/shopify-api. The Admin API supports both GraphQL and REST

I found the details on how to authenticate using the API was not very obvious though. This is how to do it.

First we need to get an Admin API access token

Login to your Shopify Dashboard. Then access apps for your store.

Then create a new app and copy the access token.

Using the access token in your code

Let’s go through an example. Let’s say we want to fetch the first three products from the store using GraphQL.

I created a .env file that looks like this

ACCESS_TOKEN=shpat_xxxx.........e21

Then we just need to install two libraries

yarn add @shopify/shopify-api dotenv

Then our code will look as follows…

import { Shopify } from '@shopify/shopify-api';
import * as dotenv from 'dotenv';
dotenv.config();

const test = async () => {
  const queryString = `{
        products (first: 3) {
          edges {
            node {
              id
              title
            }
          }
        }
      }`;

  const client = new Shopify.Clients.Graphql(
    'your-store.myshopify.com',
    process.env.ACCESS_TOKEN
  );
  const products = await client.query({
    data: queryString
  });
  console.log(products.body.data.products.edges);
};

test();

So in essence, from the Shopify docs on authenticating, you don’t need to figure out the more complex flow of OAuth.


Leave a Reply

Your email address will not be published.