Magento Tutorials

How to Create Shipment With Magento 2 API

Hello, Magento pals!

Magento 2 is a flexible, powerful E-commerce platform that enables the user to create shipping of an order with the use of API. hence, I am here with this complete tutorial on how you can create shipping with Magento 2 API.

Let’s get going!

Create Shipment With Magento 2 REST API

Get the order_item_id of all the products that are to be shipped in order to create a shipment with Magento 2. Note that:

  • To create a partial shipment, define only the order_item_ids that are going to be shipped now.
  • If the call is successful on a full shipment, The order status is changed to Complete.

Endpoint:

POST https://domain.com/rest/<store_code>/V1/order/1/ship

Here, 1 is Your Order Id and store_code (optional).

Headers:

Content-Type: application/json

Authorization: Bearer <administrator token>

Payload:

{
  "items": [
    {
      "order_item_id": 1,
      "qty": 1
    },
    {
      "order_item_id": 2,
      "qty": 2
    },
    {
      "order_item_id": 3,
      "qty": 3
    }
  ],
  "notify": true,
  "appendComment": true,
  "comment": {
    "comment": "Shipment from the warehouse"
  },
  "tracks": [
     {
       "track_number": "456456465",
       "title": "fedex Title Here",
       "carrier_code": "fedex"
     }
   ]
}

Response:

The shipment ID, such as 3.

Check the results

  1. Go to Sales > Shipments in the admin panel. The Three shipments for this order are displayed in the grid.
  2. Go to Catalog > Products. Check that the Quantity per Source values are correct for each product, based on the selections you made at shipment.

Create Sample Example for shipment using  REST API so create shipment_rest_api.php in your Magento root path after adding the below code.

<?php

$admindata = array("username" => "admin", "password" => "admin@123");

$ch = curl_init("http://domain.com/rest/V1/integration/admin/token");

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($admindata));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Length: " . strlen(json_encode($admindata))));

$token = curl_exec($ch);

curl_close($ch);

echo "Token : ".$token;

$ch = curl_init();

if (!$ch)

{

    return "Couldn't initialize a cURL handle";

}

$data = '{

  "items": [

    {

      "order_item_id": 4,

      "qty": 1

    }

  ]

}';

$host= "http://domain.com/rest/V1/order/4/ship";

curl_setopt($ch, CURLOPT_URL,$host);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));

$curlresponse = curl_exec($ch);

print_r(curl_error($curlresponse));

$responseData=json_decode($curlresponse);

echo "<pre>";

print_r($responseData);

echo "</pre>";

After adding the above code, run the below URL to check shipment was created or not.

http://domain.com/shipment_rest_api.php

Bottom Line

The aforementioned are the comprehensive details on How to Create Shipment With Magento 2 API. we anticipate that this blog post was a beneficial one for what you were looking for. 

If you have any complications or want to talk about something relevant to this problem, please write us in the comments below. I look forward to hearing from you!

Happy Coding!

Click to rate this post!
[Total: 3 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

View Comments

  • hi, how to get all order ID with tracking number with date only using api like rest/V1/shipments?searchCriteriaXXXXXXXXXXXXXXXXXX

Recent Posts

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

2 days ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

2 days ago

Top 10 Tips to Hire Shopify Developers

As the world of eCommerce continues to thrive, Shopify has become one of the most…

5 days ago

Managing Browser Events and Navigation in Shopify Remix: useBeforeUnload, useHref, and useLocation Hooks

Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…

5 days ago

Ultimate Guide to Hiring a Top Shopify Development Agency

Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…

6 days ago

How to Choose the Best Shopify Development Company?

In today’s digital landscape, e-commerce has become a cornerstone for businesses looking to thrive. Among…

6 days ago