Magento 2: How to login as Customer and place new order via REST APIs

In order to place a new order as a logged in customer using REST APIs in Magento 2, please follow below steps:

You can Download the Postman Tool from here: www.postman.com
Now, Open Post man and create below Requests: MAGENTO 2 REST APIs:

CUSTOMER LOGIN:
Request Type: POST
Request URL: {{baseUrl}}/{{storecode}}/V1/integration/customer/token
Body:
{
"username": {{customerNumber}},
"password": {{customerPassword}}
}
Headers:
Content-Type: application/json
Authorization: Bearer {{token}}
Response:
CustomerToken
view raw Login hosted with ❤ by GitHub

CREATING CUSTOMER QUOTE:
Request URL: {{baseUrl}}/{{storecode}}/V1/carts/mine
Request Type: POST
Headers:
Authorization: Bearer {{customerToken}}
Response:
Quote ID

ADD ITEMS TO CART
Request URL: {{baseUrl}}/{{storecode}}/V1/carts/mine/items
Request Type: GET
Headers:
Content-Type: application/json
Authorization: Bearer {{customerToken}}
Body:
{
"cartItem": {
"sku": "item-sku-1",
"qty": 1,
"quote_id": {{cartId}}
}
}
view raw gistfile1.txt hosted with ❤ by GitHub

COLLECT CART TOTALS
Request URL: {{baseUrl}}/{{storecode}}/V1/carts/mine/totals
Request Type: GET
Headers:
Content-Type: application/json
Authorization: Bearer {{customerToken}}
Authorization Type: Inherit Auth from Parent
view raw gistfile1.txt hosted with ❤ by GitHub

GET AVAILABLE SHIPPING METHODS & ESTIMATING SHIPPING CHARGES
Request URL: {{baseUrl}}/{{storecode}}/V1/carts/mine/estimate-shipping-methods
Request Type: POST
Headers:
Content-Type: application/json
Authorization: Bearer {{customerToken}}
Body:
{
"cartId": {{cartId}},
"address": {
"region": {{region_title}},
"region_id": {{region_id}},
"region_code": {{region_code}},
"country_id": {{country_id}},
"street": [
"123 Oak Ave"
],
"postcode": "10577",
"city": {{city_id}},
"firstname": "Reena",
"lastname": "Parekh",
"customer_id": {{customerNumber}},
"email": {{customerEmail}},
"telephone": {{customerNumber}},
"same_as_billing": 1
}
}
view raw test.txt hosted with ❤ by GitHub

SET BILLING AND SHIPPING ADDRESS OF ORDER
Request URL: {{baseUrl}}/{{storecode}}/V1/carts/mine/shipping-information
Request Type: POST
Body:
{
"addressInformation": {
"shipping_address": {
"region": {{region_title}},
"region_id": {{region_id}},
"region_code": {{region_code}},
"country_id": {{country_code}},
"street": [
"123 Oak Ave"
],
"postcode": "10577",
"city": {{city_code}},
"firstname": "Reena",
"lastname": "Parekh",
"email": {{customerEmail}},
"telephone": {{customerNumber}}
},
"billing_address": {
"region": {{region_title}},
"region_id": {{region_id}},
"region_code": {{region_code}},
"country_id": {{country_code}},
"street": [
"123 Oak Ave"
],
"postcode": "10577",
"city": {{city_code}},
"firstname": "Reena",
"lastname": "Parekh",
"email": {{customerEmail}},
"telephone": {{customerNumber}}
},
"shipping_carrier_code": "freeshipping",
"shipping_method_code": "freeshipping"
}
}
view raw test.txt hosted with ❤ by GitHub

PLACE ORDER
Request URL: {{baseUrl}}/{{storecode}}/V1/carts/mine/payment-information
Request Type: POST
Headers:
Content-Type: application/json
Authorization: Bearer {{customerToken}}
Body:
{
"paymentMethod": {
"method": "cashondelivery"
},
"billing_address": {
"region": {{region_title}},
"region_id": {{region_id}},
"region_code": {{region_code}},
"country_id": {{country_id}},
"street": [
"123 Oak Ave"
],
"postcode": "10577",
"city": {{city_id}},
"firstname": "Reena",
"lastname": "Parekh",
"email": {{customerEmail}},
"telephone": {{customerNumber}}
}
}
view raw test.txt hosted with ❤ by GitHub

Now check your admin panel and there will be a new COD order placed.

Comments

Popular posts from this blog

SEO for your Press Releases [Updated 2025 list]

XDebug - PHPStorm - Mac: How to configure Xdebug with PHPStorm on Mac OS and PHP 7.3