How to access Magento 2 API using curl command
When i first time trying to access to Magento 2 API using CLI curl command. I couldn’t find any good reference or example. So i have decided to write my own reference for future.
You can access Magento 2 API in two simple steps. Unlike from Magento 1 you don’t have to generate separate API credential you can store admin credentials to access.
Step 1: Generate Token
curl -X POST "http://magento2.test/rest/V1/integration/admin/token" \
-H "Content-Type:application/json" \
-d "{ \"username\":\"adminUsername\", \"password\":\"accountPassword\"} "
Expected output is a random string like this. Its your session token and will be used in further access
"gow7n7etw5lckj6hvc20ngs32z949w11"
Step 2: Access customer information
Using the same token in header that we received in earlier API call
curl -X GET "http://magento2.test/rest/V1/products/1" \
-H "Authorization: Bearer gow7n7etw5lckj6hvc20ngs32z949w11"
Expected output will be the customer information in JSON format.
{
"id": 1,
"group_id": 1,
"created_at": "2019-08-14 09:22:41",
"updated_at": "2019-08-14 09:22:42",
"created_in": "Default Store View",
"email": "[email protected]",
"firstname": "Anuj",
"lastname": "Kaushal",
"website_id": 1,
"addresses": [],
"disable_auto_group_change": 0,
"extension_attributes": {
"is_subscribed": false
}