How to Create a Customer Using Magento 2 API

There are three ways which customers could make purchases:

  • Log in before making the purchase
  • Log in or create an account when the order is placed
  • Not create an account when making purchases

Though it’s convenient for customers to make purchase by the methods they prefer. If customers doesn’t create an account on your store, It might be difficult for you to get information to build a customer base. However, you can create customer accounts on behalf of them. It’s easy than you think through this easy-to-understand tutorial.

In this tutorial, an order is created by a user who has already logged in. With guest users, additional REST endpoints are provided to handle them.

But before doing that, let’s call API to create a customer by following these steps.

Create a Customer using Magento 2 API in 3 steps:

Step 1: Create a customer account

To create a customer account, admin permissions are required.

Note: In this example, a simplified way of creating a customer account will be shown. Instead of a customer password would be defined using plain text, in here the payload will be specified without the password parameter. In the default setting, when the call is successful, a Welcome email which includes a request to set the password will be sent to the customer. Also, by calling PUT /V1/customers/password, you can initiate a password reset email.

Endpoint

POST <host>/rest/<store_code>/V1/customers

Headers

Content-Type application/json

Authorization Bearer <admin token>

Payload

You should substitute the value of the email parameter with a real email address. As if you do that, you can receive all notifications.

Code sample

{
    "customer": {
        "email": "jdoe@example.com",
        "firstname": "Jane",
        "lastname": "Doe",
        "addresses": [{
            "defaultShipping": true,
            "defaultBilling": true,
            "firstname": "Jane",
            "lastname": "Doe",
            "region": {
                "regionCode": "NY",
                "region": "New York",
        "regionId":43
            },
            "postcode": "10755",
            "street": ["123 Oak Ave"],
            "city": "Purchase",
            "telephone": "512-555-1111",
            "countryId": "US"
        }]
    },
  "password": "Password1"
}

Response

This user id value of 2 is assigned by Magento.

Code sample

{
  "id": 2,
  "group_id": 1,
  "default_billing": "2",
  "default_shipping": "2",
  "created_at": "2017-01-31 01:18:13",
  "updated_at": "2017-01-31 01:18:13",
  "created_in": "Default Store View",
  "email": "jdoe@example.com",
  "firstname": "Jane",
  "lastname": "Doe",
  "store_id": 1,
  "website_id": 1,
  "addresses": [
    {
      "id": 2,
      "customer_id": 2,
      "region": {
        "region_code": "NY",
        "region": "New York",
        "region_id": 43
      },
      "region_id": 43,
      "country_id": "US",
      "street": [
        "123 Oak Ave"
      ],
      "telephone": "512-555-1111",
      "postcode": "10755",
      "city": "Purchase",
      "firstname": "Jane",
      "lastname": "Doe",
      "default_shipping": true,
      "default_billing": true
    }
  ],
  "disable_auto_group_change": 0
}

You can use the username jdoe@example.com and the password Password1 to log in to the Luma store

Step 2: Get the customer’s access token

In order to get a customer’s access token, the customer’s username and password in the payload must be specified. Specifying an admin authorization token is not necessary.

In the default setting, a customer token’s valid time is 1 hour. If you want to change this value, please log in to your Admin panel and then go to Stores > Settings > Configuration > Services > OAuth > Access Token Expiration.

Endpoint

POST <host>/rest/<store_code>/V1/integration/customer/token

Headers

Content-Type application/json

Payload

{
"username": "jdoe@example.com",
"password": "Password1"
}

Response

The customer’s access token is returned by Magento. It has to be specified in the authorization header of every call which the customer makes on his or her own behalf.

q0u66k8h42yaevtchv09uyy3y9gaj2ap

Step 3: Verify this step

  1. Using the email jdoe@example.com and password Password1 to log in to the Luma website.
  2. Click on the account name which is in the upper corner in your right hand and choose My Account.
  3. To view the billing and shipping addresses click Address Book.

Conclusion

Above, I have just guided you on how to call API to create a customer. I hope it is helpful for you. If you have any questions or new ideas, feel free to leave a comment below. Thanks for reading!

Image Description
With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.
x