1

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!"; }

  • 1
    you have to create your own API endpoint that accepts user username as input for search customer data. – fmsthird Feb 4 at 9:38
  • Added an answer below. If you are specifically needing a response containing token and customer id directly from logging in, @fmsthird is correct that you need a custom endpoint. As part of that endpoint you'll have to define a new interface that holds token and customer id and return that. I could probably do a write up of how that could be accomplished if it's what you are looking for :). – floorz Jun 11 at 23:08
0

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"
        }
    ]
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.