Magento – Magento 2 : Rest Api for get customer id with token number

customerrest

I need customer id and token number when login customer account in Magento2
I used this API for customer login:

Method: Post rest/V1/integration/customer/token

Body

{
"username" : "abc@gmail.com",
"password" : "abc12345!";
}

Best Answer

The following endpoint (the one you mentioned) will return the access token upon submitting correct user credentials:

Method: Post rest/V1/integration/customer/token

Then all you need to do is pass that token as a bearer token to this next endpoint and you'll receive all the details for that customer:

Method: Get rest/V1/customers/me

Example Request

curl --request GET \ --url https://example.com/rest/V1/customers/me \ --header 'authorization: Bearer x6qa7biotlej2s2li1tl3zzx5ww42lt8'

Example Response

Customer id is id in the response

{
    "id": 77819,
    "group_id": 1,
    "default_shipping": "11169",
    "created_at": "2018-08-23 15:49:17",
    "updated_at": "2019-06-11 22:33:59",
    "created_in": "US",
    "email": "testperson@somedomain.com",
    "firstname": "Test",
    "lastname": "Person",
    "store_id": 1,
    "website_id": 1,
    "addresses": [
        {
            "id": 11170,
            "customer_id": 77819,
            "region": {
                "region_code": "Etelä-Savo",
                "region": "Etelä-Savo",
                "region_id": 976
            },
            "region_id": 976,
            "country_id": "FI",
            "street": [
                "Finlandiantie"
            ],
            "telephone": "1234",
            "postcode": "58450",
            "city": "Punkaharju",
            "firstname": "Test",
            "lastname": "Person",
            "custom_attributes": [
                {
                    "attribute_code": "some_custom_attribute",
                    "value": "A custom value"
                }
            ]
        }
    ],
    "disable_auto_group_change": 0,
    "extension_attributes": {
        "is_subscribed": false
    },
    "custom_attributes": [
        {
            "attribute_code": "some_custom_attribute",
            "value": "A custom value"
        }
    ]
}