Jigar KarangiyaJigar Karangiya

How to Create a Customer Account Using Magento 2 GraphQL

JK
Jigar Karangiya
· 3 min read
How to Create a Customer Account Using Magento 2 GraphQL

Creating a customer account is one of the most essential operations in any eCommerce store. Magento 2 supports this via REST and GraphQL APIs. In this blog, we’ll explore how to create a customer account using GraphQL in Magento 2 with examples, query structure, and best practices.

Whether you’re a developer integrating a mobile app or building a headless frontend with React or Vue, this guide will help you get started.


Why Use GraphQL for Customer Operations?

Magento 2 introduced full GraphQL support for storefront operations, which allows frontend developers to:

  • Request only the data needed
  • Improve performance with fewer API calls
  • Simplify integration for mobile apps and PWA Studio

Creating a customer account via GraphQL is clean, efficient, and fast.


Prerequisites

Before we dive into the query, make sure:

  • Your Magento 2 store is version 2.3.3 or above (GraphQL stable)

  • GraphQL module is enabled

  • A GraphQL client like Altair , Postman , or Magento PWA Studio is ready

  • Magento base URL is accessible, typically like:

    https://yourstore.com/graphql


GraphQL Mutation to Create a Customer Account

Magento provides a mutation called createCustomer to register new accounts.

graphql
mutation {
  createCustomer(
    input: {
      firstname: "John"
      lastname: "Doe"
      email: "john.doe@example.com"
      password: "StrongPassword123"
    }
  ) {
    customer {
      id
      firstname
      lastname
      email
    }
  }
}

The field Mutation.createCustomer is deprecated. Use createCustomerV2 instead.

In this blog, We will use createCustomerV2 mutation.

Request

graphql
mutation {
  createCustomerV2(
    input: {
      firstname: "Jigar"
      lastname: "Karangiya"
      email: "jigarkarangiyablogs@gmail.com"
      password: "b0bl0bl@w"
      is_subscribed: true
    }
  ) {
    customer {
      firstname
      lastname
      email
      is_subscribed
    }
  }
}

Response

json
{
  "data": {
    "createCustomer": {
      "customer": {
        "firstname": "Jigar",
        "lastname": "Karangiya",
        "email": "jigarkarangiyablogs@gmail.com",
        "is_subscribed": true,
      }
    }
  }
}

As of version 2.4.7, you can use the custom_attributes field to define an array of custom attributes to apply to the customer.


Create customer account with custom attributes

Request

graphql
mutation {
  createCustomerV2(
    input: {
      firstname: "Jigar"
      lastname: "Karangiya"
      email: "jigar.karangiya@test.com"
      password: "sjjsjs@444js#"
      is_subscribed: true
      custom_attributes: [
        {
          attribute_code: "alternative_email"
          value: "jigar.karangiya@2ndemail.com"
        },
        {
          attribute_code: "certificates"
          value: "992,995"
          selected_options: [
            {
              uid: "Adobe Commerce - Certified Professional Developer"
              value: "992"
            },
            {
              uid: "Adobe Commerce - Certified Expert Developer"
              value: "995"
            }
          ]
        }
      ]
    }
  ) {
    customer {
      firstname
      lastname
      email
      is_subscribed
      custom_attributes {
        code
        ... on AttributeValue {
          value
        }
        ... on AttributeSelectedOptions {
          selected_options {
            label
            value
          }
        }
      }
    }
  }
}

Response

json
{
  "data": {
    "createCustomer": {
      "customer": {
        "firstname": "Jigar",
        "lastname": "Karangiya",
        "email": "jigar.karangiya@test.com",
        "is_subscribed": true,
        "custom_attributes": [
          {
            "code": "alternative_email",
            "value": "jigar.karangiya@2ndemail.com"
          },
          {
            "code": "certificates",
            "selected_options": [
              {
                "label": "Adobe Commerce - Certified Professional Developer",
                "value": "992"
              },
              {
                "label": "Adobe Commerce - Certified Expert Developer",
                "value": "995"
              }
            ]
          }
        ]
      }
    }
  }
}

Input attributes

You can use the following attributes as input for the createCustomerV2 mutation.

Field NameDescription
allow_remote_shopping_assistanceBooleanIndicates whether the customer has enabled remote shopping assistance.
custom_attributes[AttributeValueInput]The customer’s custom attributes.
date_of_birthStringThe customer’s date of birth.
dobString
emailString!The customer’s email address.
firstnameString!The customer’s first name.
genderIntThe customer’s gender (Male – 1, Female – 2).
is_subscribedBooleanIndicates whether the customer is subscribed to the company’s newsletter.
lastnameString!The customer’s family name.
middlenameStringThe customer’s middle name.
passwordStringThe customer’s password.
prefixStringAn honorific, such as Dr., Mr., or Mrs.
suffixStringA value such as Sr., Jr., or III.
taxvatStringThe customer’s Tax/VAT number (for corporate customers).

Common Response Errors

ErrorDescription
A customer with the same email address already exists in an associated website.The email provided in the input.email argument belongs to an existing customer.
"Email" is not a valid email address.The value provided in the input.email argument has an invalid format.
Field CustomerInput.email of required type String! was not providedThe input.email argument was omitted.
Field "xxx" is not defined by type CustomerInput.The input.xxx argument is undefined.
Required parameters are missing: First NameThe input.firstname argument was omitted or contains an empty value.

Creating a customer account via Magento 2 GraphQL is straightforward, clean, and ideal for headless and modern frontend setups. With the power of GraphQL, developers can craft highly optimized experiences while keeping control of what data is sent or received.

You may also like,

How to Generate a Customer Token Using Magento 2 GraphQL

Written by Jigar Karangiya, Adobe Commerce & Magento 2 developer.

More about me

Related posts