I trust that you found this blog post to be enjoyable. If you are interested in having my team handle your eCommerce setup and marketing for you, Contact Us Click here.

How to create Metafield for a Customer In Shopify Using Api

How to create Metafield for a Customer In Shopify Using Api

You can use the Metafield resource to add additional information to other Admin API resources. Metafields serve various purposes, such as adding a summary to a blog article or sharing data with other Shopify apps. I'll demonstrate how to create a metafield for a customer using the Shopify API in Postman

Step 1: Generate API credentials from the Shopify admin

  • Log in to your Shopify admin
  • Go to settings – Apps and sales channels

 

 

  • Click on Develop Apps
  • After then you will see this screen then click on Allow custom app development.
  • And if you have already created a custom app, you will see another screen. I am attaching both screens.
  • In the "App Details" section, enter a name for your app and provide an emergency developer email.
  • After filling out the details section, you need to click on "Create App." Then, you will see a new screen to configure the Admin API scope.
ptiwebtech_website
ptiwebtech_website
ptiwebtech_website
  • In the Admin API section, select the areas of your store that you want the app to access.
  • After making your selections, click on "Save" to set the scope you require.
  • After then install your app
ptiwebtech_website
  • After installing the app, you will see a new API credential screen. Then, reveal the token and save it.you’ll use these credentials to make authorized request
ptiwebtech_website

Step 2: Retrieve customer id with Postman

  •  You can use the Admin API access token you generated for your request’s basic HTTP  authorization.
  • https://{shop}.myshopify.com/admin/api/{api-version}/{resource}.json
  • {shop} – The name of your development store.
  • {api-version} – The supported API version that you want to use.
  • {resource} – A resource endpoint from the REST Admin API.

Log in to your Postman and create a new workspace.Paste the URL above and set the header. Example - Then you find the customer id you want to create metafield and copy its ID.

ptiwebtech_website

{

"customers": [

  {

           "id": 5828166582369,

           "email": "jay.p1@ptiwebtech.com",

           "created_at": "2022-06-27T03:47:14-04:00",

           "updated_at": "2022-07-27T01:38:02-04:00",

           "first_name": null,

           "last_name": null,

           "orders_count": 0,

           "state": "disabled",

           "total_spent": "0.00",

           "last_order_id": null,

           "note": null,

           "verified_email": true,

           "multipass_identifier": null,

           "tax_exempt": false,

           "tags": "",

           "last_order_name": null,

           "currency": "INR",

           "phone": null,

           "addresses": [],

           "accepts_marketing": false,

           "accepts_marketing_updated_at": null,

           "marketing_opt_in_level": "single_opt_in",

           "tax_exemptions": [],

           "email_marketing_consent": {

               "state": "not_subscribed",

               "opt_in_level": "single_opt_in",

               "consent_updated_at": null

           },

           "sms_marketing_consent": null,

           "admin_graphql_api_id": "gid://shopify/Customer/5828166582369"

       }

   ]

}

Step 3: Create metafield for a customer with customer id

Open a new tab in Postman and change the URL with this Endpoint.

POST

https://{shop}.myshopify.com/admin/api/{api-version}/customers/{customer_id}/metafields.json

In the Body section, click raw and choose JSON and also set header with as per previous image.

Copy this code paste in body

{

   "metafield": {

       "namespace": "customer",

       "key": "customer_metafield_test",

       "value": "customer metafield created",

       "type": "single_line_text_field"

   }

}

ptiwebtech_website

You can view the response after creating a customer metafield.

  • {namesapce}A container for a set of metafields. You must define a custom namespace for your metafields to distinguish them from the metafields used by other apps
  • {key} The name of the metafield must have a minimum length of 3 characters and a maximum length of 30 characters.
  • {value}The information to be stored as metadata.
  • {type}The metafield's information type. See the full list of types for reference.

     

     

    You can check Response after Creating Metafield

    {

       "metafield": {

           "id": 29858127741025,

           "namespace": "customer",

           "key": "customer_metafield_test",

           "value": "customer metafield created",

           "description": null,

           "owner_id": 5828166582369,

           "created_at": "2024-05-02T03:49:43-04:00",

           "updated_at": "2024-05-02T03:49:43-04:00",

           "owner_resource": "customer",

           "type": "single_line_text_field",

           "admin_graphql_api_id": "gid://shopify/Metafield/29858127741025"

       }

    }

    Back to blog