Magento 2: Some examples of How to use Magento 2 APIs

This is a very short tutorial to demonstrate how to use Magento2 APIs to create orders and update source items

Posted on February 21, 2019 in Magento2

Use Magento 2 APIs to Create an Order


<?php
$access_token = '4v81lfihtkqanfdr6798zj352qyuyx1x';

//Get Cart Id String
$url = 'http://magento2.local/index.php/rest/V1/guest-carts/';

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer ' . $access_token));

$result = curl_exec($ch);

$cartId = str_replace('"', '', $result);



//Get Quote Id and Item Id
$body = json_encode(['cartItem' => ['quote_id' => $cartId, 'qty' => 1, 'sku' => '24-MB04']]);

$url = 'http://magento2.local/index.php/rest/V1/guest-carts/'. $cartId . '/items';

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer ' . $access_token));

$result = curl_exec($ch);

$obj = json_decode($result);

var_dump($obj);

$body = json_encode([ 'addressInformation' =>[
    'shippingAddress' => [
        "country_id" => 'US',
        "street" =>[
            "123 Main Str"
        ],
        "company" => "Company",
        "telephone" => "123456",
        "postcode" => "91780",
        "city" => "El Monten",
        "firstname" => "Guo",
        "lastname" => "Jing",
        "region_id" => "12",
        "region" => "California",
        "email" => "dummy@example.com",
        "sameAsBilling" => 1
    ],
    'billingAddress' => [
        "country_id" => 'US',
        "street" =>[
            "123 Main Str"
        ],
        "company" => "Company",
        "telephone" => "123456",
        "postcode" => "91780",
        "city" => "El Monten",
        "firstname" => "Guo",
        "lastname" => "Jing",
        "region_id" => "12",
        "region" => "California",
        "email" => "dummy@example.com",
    ],
    "shipping_method_code" => "flatrate",
    "shipping_carrier_code" => "flatrate" 
    ]
]);

$url = 'http://magento2.local/index.php/rest/V1/guest-carts/'. $cartId . '/shipping-information';

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer ' . $access_token));

$result = curl_exec($ch);

var_dump($result);

$body = json_encode([
    "paymentMethod" => [ 
        "method" => "checkmo"
    ]
]);
$url = 'http://magento2.local/index.php/rest/V1/guest-carts/'. $cartId . '/order';

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer ' . $access_token));

$result = curl_exec($ch);

var_dump($result);

curl_close($ch);

Create Or Update Source Items


<?php
$access_token = '4v81lfihtkqanfdr6798zj352qyuyx1x';

$url = 'http://magento2.local/index.php/rest/V1/inventory/source-items';

$ch = curl_init($url);

$body = json_encode(['sourceItems' =>[
        ['source_code' => 'default', 'sku' => '24-MB01', 'quantity'=>555, 'status'=>1],
        ['source_code' => 'la', 'sku' => '24-MB01', 'quantity' => 222, 'status' => 1],
        ['source_code' => 'default', 'sku' => '24-MB04', 'quantity'=>444, 'status'=>1],
        ['source_code' => 'la', 'sku' => '24-MB04', 'quantity' => 111, 'status' => 1],
        ['source_code' => 'default', 'sku' => '24-MB03', 'quantity'=>333, 'status'=>1],
        ['source_code' => 'la', 'sku' => '24-MB03', 'quantity' => 111, 'status' => 1],
        ['source_code' => 'default', 'sku' => '24-MB02', 'quantity'=>222, 'status'=>1],
        ['source_code' => 'la', 'sku' => '24-MB02', 'quantity' => 100, 'status' => 1],
        ['source_code' => 'default', 'sku' => '24-MB06', 'quantity'=>666, 'status'=>1],
        ['source_code' => 'la', 'sku' => '24-MB06', 'quantity' => 110, 'status' => 1],
        ['source_code' => 'default', 'sku' => '24-MB05', 'quantity'=>555, 'status'=>1],
        ['source_code' => 'la', 'sku' => '24-MB05', 'quantity' => 101, 'status' => 1],
    ]]);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer ' . $access_token));

$result = curl_exec($ch);

var_dump($result);

$obj = json_decode($result);

var_dump($obj);

curl_close($ch);

Get the Access Token

In the previous examples, I have used the $access_token to make API’s call. You may ask how to get this value. Here is it:


$url = 'http://magento2.local/index.php/rest/V1/integration/admin/token';

$ch = curl_init($url);

$body = json_encode(['username' => 'your username', 'password' => 'your password']);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', "Content-Lenght: " . strlen($body)));

$access_token = curl_exec($ch);

echo $access_token . "\n\n\n";

I hope these examples will help you in your future project. You can view all Magento 2 APIs with:
custom linked product


comments powered by Disqus