Error in creating a customer and adding a specific locale

Can someone tell me if this is correct?

the customer is created but it seems that the locale is not updated:

import axios from 'axios';
import dotenv from 'dotenv';

dotenv.config();

const MAX_RETRIES = 3;
const INITIAL_RETRY_DELAY = 5000; // 5 seconds

console.log('Storefront Access Token:', process.env.NEXT_SHOPIFY_STOREFRONT_ACCESS_TOKEN);
console.log('Admin Access Token:', process.env.NEXT_SHOPIFY_ADMIN_ACCESS_TOKEN);
console.log('Store Domain:', process.env.NEXT_SHOPIFY_STORE_DOMAIN);

// Function to create a customer

async function createCustomer(customerData) {
  const shopifyStorefrontAccessToken = process.env.NEXT_SHOPIFY_STOREFRONT_ACCESS_TOKEN;
  const shopDomain = process.env.NEXT_SHOPIFY_STORE_DOMAIN;

  try {
    const response = await axios({
      url: `https://${shopDomain}/api/2024-04/graphql.json`,
      method: 'post',
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        'X-Shopify-Storefront-Access-Token': shopifyStorefrontAccessToken,
      },
      data: {
        query: `
          mutation customerCreate($input: CustomerCreateInput!) {
            customerCreate(input: $input) {
              customer {
                id
              }
              userErrors {
                field
                message
              }
            }
          }
        `,
        variables: {
          input: customerData
        }
      }
    });

    return response.data;
  } catch (error) {
    throw new Error('Error creating customer:', error);
  }
}

// Function to update customer locale
async function updateCustomerLocale(customerId, locale) {
  const adminAccessToken = process.env.NEXT_SHOPIFY_ADMIN_ACCESS_TOKEN;
  const shopDomain = process.env.NEXT_SHOPIFY_STORE_DOMAIN;

  try {
    const response = await axios({
      url: `https://${shopDomain}/admin/api/2024-04/graphql.json`,
      method: 'post',
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        'X-Shopify-Access-Token': adminAccessToken,
      },
      data: {
        query: `
          mutation customerUpdate($input: CustomerInput!) {
            customerUpdate(input: $input) {
              customer {
                id
                locale
              }
              userErrors {
                field
                message
              }
            }
          }
        `,
        variables: {
          input: {
            id: customerId,
            locale: locale
          }
        }
      }
    });

    console.log('Update Locale Response:', JSON.stringify(response.data, null, 2));

    if (response.data.errors || response.data.data.customerUpdate.userErrors.length > 0) {
      throw new Error(JSON.stringify(response.data.errors || response.data.data.customerUpdate.userErrors));
    }

    return response.data.data.customerUpdate.customer;
  } catch (error) {
    console.error('Error updating customer locale in Shopify:', error);
    if (error.response) {
      console.error('Error response status:', error.response.status);
      console.error('Error response headers:', JSON.stringify(error.response.headers, null, 2));
      console.error('Error response data:', JSON.stringify(error.response.data, null, 2));
    } else {
      console.error('No response from server');
    }

    throw error;
  }
}

export default async (req, res) => {
  if (req.method !== 'POST') {
    return res.status(405).json({ message: 'Method not allowed' });
  }

  const { firstName, lastName, email, password, acceptPrivacy, acceptNewsletter, locale } = req.body;

  try {
    const customerData = {
      firstName,
      lastName,
      email,
      password,
      acceptsMarketing: acceptNewsletter,
    };

    const createdCustomerResponse = await createCustomer(customerData);
    if (createdCustomerResponse.data.customerCreate.userErrors.length > 0) {
      return res.status(400).json({ success: false, errors: createdCustomerResponse.data.customerCreate.userErrors });
    }

    const customer = createdCustomerResponse.data.customerCreate.customer;
    const updatedCustomer = await updateCustomerLocale(customer.id, locale);

    return res.status(201).json({ success: true, customer: updatedCustomer });
  } catch (error) {
    console.error('API error:', error.message);
    return res.status(500).json({ success: false, message: error.message });
  }
};

thank you!

Hey @delucas87 ,

Just looking here, I don’t see anything specific with the API requests that would be causing it to not update. It’s working testing on my end here.

One suggestion is to add some additional logging in the update request to gather more details from the response headers. Also, since the update is using the admin API, double check that your app has the permissions for updating customers.

Hope that helps,

  • Kyle G.