magento2magento-rest-api

Magento 2 - Request sample for carts/mine (GET)


I try to call the carts/mine GET route of the Magento 2 REST API, but it does not show a request sample in the documentation.

enter image description here

It says "Returns information for the cart for a specified customer."

But how do I specify the customer?


Solution

  • You should include a customer bearer to call this API. You can get it from calling /integration/customer/token. Here is an example:

    POST https://<host>/rest/V1/integration/customer/token
    Content-Type: application/json
    
    {"username":"<email>", "password":"<password>"}
    

    It will return a customer token looks like this:

    "3zznrpsthn6m3dheeacyfe3xzdjryd3y"
    

    Then use this bearer token to call cart/mine API:

    GET {{host}}/rest/V1/carts/mine/
    Authorization: Bearer 3zznrpsthn6m3dheeacyfe3xzdjryd3y
    Content-Type: application/json
    

    And this is an example response:

    {
      "id": 3354,
      "created_at": "2023-02-15 03:44:37",
      "updated_at": "2023-02-15 03:44:37",
      "is_active": true,
      "is_virtual": false,
      "items": [],
      "items_count": 0,
      "items_qty": 0,
      "customer": {
        "id": 145,
        "group_id": 1,
        "default_billing": "140",
        "default_shipping": "140",
        "created_at": "2021-09-13 06:47:49",
        "updated_at": "2022-10-11 08:03:42",
        "created_in": "en_US",
        "email": "????",
        "firstname": "????",
        "lastname": "????",
        "store_id": 2,
        "website_id": 1,
        "addresses": [
          {
            "id": 140,
            "customer_id": 145,
            "region": {
              "region_code": "????",
              "region": "????",
              "region_id": 860
            },
            "region_id": 860,
            "country_id": "??",
            "street": [
              "???"
            ],
            "telephone": "????",
            "city": "????",
            "firstname": "????",
            "lastname": "????",
            "default_shipping": true,
            "default_billing": true,
          }
        ],
        "disable_auto_group_change": 0,
        "extension_attributes": {
          "is_subscribed": false
        }
      },
      "billing_address": {
        "id": 10058,
        "region": null,
        "region_id": null,
        "region_code": null,
        "country_id": null,
        "street": [
          ""
        ],
        "telephone": null,
        "postcode": null,
        "city": null,
        "firstname": null,
        "lastname": null,
        "customer_id": 145,
        "email": "????",
        "same_as_billing": 0,
        "save_in_address_book": 0
      },
      "orig_order_id": 0,
      "currency": {
        "global_currency_code": "HKD",
        "base_currency_code": "HKD",
        "store_currency_code": "HKD",
        "quote_currency_code": "HKD",
        "store_to_base_rate": 0,
        "store_to_quote_rate": 0,
        "base_to_global_rate": 1,
        "base_to_quote_rate": 1
      },
      "customer_is_guest": false,
      "customer_note_notify": true,
      "customer_tax_class_id": 3,
      "store_id": 1,
      "extension_attributes": {
        "shipping_assignments": []
      }
    }