Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

Shopify admin Rest Api calls in Netlify lambda function

Shopify admin Rest Api calls in Netlify lambda function

csdk
Shopify Partner
1 0 0

Trying to create a new user using the Shopify admin Api.

According to the shopify guide, I generate a string in base64 and pass in headers as Authorization property

 

const axios = require('axios');

exports.handler = async (event) => {
  try {
    const endpoint = `https://${process.env.GATSBY_SHOPIFY_STORE_URL}/admin/api/2021-10/customers.json`;

    const config = {
      headers: {
        'Content-Type': 'application/json',
        Authorization: `Basic ${Buffer.from(`${process.env.SHOPIFY_ADMIN_API_KEY}:${process.env.SHOPIFY_ADMIN_PASSWORD}`
        ).toString('base64')}`,
      },
    };

    const data = JSON.stringify({
      customer: {
        first_name: event.body.userName || 'Subscriber',
        email: event.body.userEmail,
        accepts_marketing: true,
        marketing_opt_in_level: 'confirmed_opt_in',
      },
    });

    const { statusCode, statusText } = await axios.post(endpoint, { data, config });

    return { statusCode, statusText };
  } catch (error) {
    return { statusCode: 404, statusText: error.message };
  }
};

 

Using this approach, I always get 401 code 

 

response: {
    status: 401,
    statusText: 'Unauthorized',
    data: { 
      errors: '[API] Invalid API key or access token (unrecognized login or wrong password)'
 },
   isAxiosError: true,
}

 

Has anyone encountered this? If so, it would be great if they clarified. Thanks 😊

Replies 0 (0)