Skip to content

Magento 2: How to add a tracking number to an order using REST API

I use the following code to add a tracking number to an order:

        $url = 'https://xxxxx/rest/V1/order/514/ship';

        $authorization = "Authorization: Bearer ".$json; //I get $json in other part of the code, this work fine
    
        $data = [
            "items" => [
                [
                    "order_item_id" => 614,
                    "qty" => 1
                ]
            ],

            "tracks" => [
                "track_number" =>"1111111111",
                "title" => "Pedido enviado", 
                "carrier_code" => "DHL"
            ]
        ];

        $data_string = json_encode($data);

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);            
        $result = curl_exec($curl);
        curl_close($curl);
        $json2 = json_decode($result);

But I get the following error:

“Shipment Document Validation Error(s): Please, introduce a tracking number.

What is the problem?

Thank you very much!!!