Making API calls to Admin API from server via Axios

Highlighted
Tourist
8 0 0

I am unable to call the Admin REST API from my Node server via the Axios lib.

 

Trying like this:

const axios = require("axios")
const assemble_admin_url = require("./assemble_admin_url")
const {
  shopify_secret: {
    shopify_email,
    shopify_password
  },
  shopify_public: {
    version,
    shop_name
  }
} = require("../../config")

const shopify_axios_instance = axios.create({
  baseURL: assemble_admin_url(shop_name, version), // Looks like https://${shop_name}.myshopify.com/admin/api/${shopify_api_version}
auth: { username: shopify_email, password: shopify_password }, }); module.exports = async (req, res) => { try { const { data: { custom_collections: collections } } = await shopify_axios_instance.get("/custom_collections.json") const { data: { collects } } = await shopify_axios_instance.get("/collects.json") const { data: { products } } = await shopify_axios_instance.get("/products.json") const collections_with_products = collections.map(collection => { const filter_products = ({ id }) => collects.filter( ({ collection_id }) => collection_id === collection.id ).some(({ product_id }) => product_id === id) return { collections, products: products.filter(filter_products) } }) res({ collections: collections_with_products, products }) } catch (error) { console.log(error) res({ collections: {}, products: {} }) } }

I keep getting: 

Request failed with status code 401

 

Also I have tried just fetching from URL 

https://{username}:{password}@{shop}.myshopify.com/admin/api/{api-version}/{resource}.json

But has the same error.

 

Is this a valid solution or how should I be calling the API?

 

Thanks in advanced!

 

Josef

 

0 Likes