How to add new customer using Shopify Client Rest?

Hi everyone,

I’m new to the Shopify REST API and currently building my own API to create new customers in my store.

So far, I’ve successfully fetch customer data, and it works perfectly using this code:

credentials here...
const client = new shopify.clients.Rest({ session: session });
const response = await client.get({
    path: "customers",
});

Now, I’m trying to create a new customer using this REST client, but I’m running into an error. Here’s what I’ve tried:

const client = new shopify.clients.Rest({ session: session });
const response = await client.post({
    path: "customers",
    data: {
        customer: {
            firstName: "John",
            lastName: "Doe",
            email: "johndoe@email.com",
        },
    },
    type: DataType.JSON,
});

The error I’m seeing says DataType is not defined, so it seems like my code structure may be incorrect.

To give some context, I’m calling this code from a custom JavaScript file in my app extension.

Any advice or pointers would be greatly appreciated.

Thanks in advance!

Hey @Devejo ,

It looks like you’re on the right track for creating a new customer using the Shopify REST API, but the error you’re encountering indicates that the DataType object is not defined in your current context. In the Shopify API client for Node.js, you typically don’t need to specify the type when making a POST request; the client should automatically handle JSON data formatting for you.

Here’s how you can modify your code to create a new customer:

const client = new shopify.clients.Rest({ session: session });

try {
    const response = await client.post({
        path: "customers",
        data: {
            customer: {
                first_name: "John", // Note the use of underscores here
                last_name: "Doe",
                email: "johndoe@email.com",
            },
        },
    });

    console.log("Customer created successfully:", response);
} catch (error) {
    console.error("Error creating customer:", error);
}

Key Changes and Tips:

-Property Names: Ensure you are using the correct property names for the customer object. In the Shopify API, the keys for the customer object are typically first_name and last_name (with underscores) instead of firstName and lastName.

-Error Handling: Wrap your API call in a try…catch block to handle any errors gracefully. This will help you debug and understand what went wrong if the API call fails.

-No DataType Specified: You don’t need to specify DataType.JSON since the client.post method handles the request’s content type for you.

-Session Management: Make sure that your session object is correctly initialized and has the necessary permissions to create customers.

-Testing the Endpoint: If you continue to encounter issues, you might want to test the API endpoint using a tool like Postman or Insomnia to ensure that your store’s API settings allow for customer creation and that you’re using valid credentials.

By following these guidelines, you should be able to successfully create a new customer in your Shopify store using the REST API. If you have further questions or run into any more issues, feel free to ask!

If I was able to help you, please don’t forget to Like and mark it as the Solution!
Best Regard,
Rajat Sharma

Hi rajweb, yeah I figured that would be the issue. Thanks for help!

1 Like

Hii @Devejo ,

Thank you for your reply. I’m glad to hear that the solution worked well for you. If you require any more help, please don’t hesitate to reach out. If you find this information useful, a Like would be greatly appreciated.