Create a product by REST API /V1/products programmatically in Magento 2.

Create a New product by Magento REST API, you need first to generate an access token. The access token is used to create a product in the admin panel. Check the link for Generate Access Token Magento 2

Once you got the Access Token for Magento, You can call REST API for creating a product from a third-party system.

You can create a simple product from a third-party platform by calling Magento REST API without touching the Magento backend.

You can programmatically create a simple product by just below code,

Action Type: POST

Request URL:  <YOUR_SITE_URL>/rest/<store_code>/V1/products

By default, the store_code value is the default. If you have modified the name of your store code you need to pass store_code in the API URL.

If you have multiple websites and want to assign products to all the websites you must need to pass “all” as store_code value the URL in API. (<YOUR_SITE_URL>/rest/all/V1/products)

Header:  For Header, You need to pass an array for headers,
‘Content-Type:application/json’,’Authorization:Bearer ‘.$accessToken’

Where $accessToken is your site Access Token.

<?php
$url = "http://127.0.0.1/magento2/index.php/rest/all";
$token_url= $url."/V1/integration/admin/token";

$username= "admin";
$password= "admin123";

//Authentication REST API magento 2,    
$ch = curl_init();
$data = array("username" => $username, "password" => $password);
$data_string = json_encode($data);

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $token_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json'
    ));
$token = curl_exec($ch);
$adminToken=  json_decode($token);
$headers = array('Content-Type:application/json','Authorization:Bearer '.$adminToken);

// Createt Product REST API URL
$apiUrl = $url."/V1/products";

$ch = curl_init();
$data = [
    "product" => [
        "sku" => "Test Product 1",
        "name" => "Test Product 1",
        "attribute_set_id" => 4,
        "price" => 99,
        "status" => 1,
        "visibility" => 2,
        "type_id" => "simple",
        "weight" => "1",
        "extension_attributes" => [
            "category_links" => [
                [
                    "position" => 0,
                    "category_id" => "5"
                ],
                [
                    "position" => 1,
                    "category_id" => "7"
                ]
            ],
            "stock_item" => [
                "qty" => "1000",
                "is_in_stock" => true
            ]
        ],
        "custom_attributes" => [
            [
                "attribute_code" => "description",
                "value" => "Description of product here"
            ],
            [
                "attribute_code" => "short_description",
                "value" => "short description of product"
            ]
        ]
    ]
];
$data_string = json_encode($data);

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);

$response = json_decode($response, TRUE);
curl_close($ch);

In the above Request, We have created a simple product in Magento 2, Pass the required data to create a simple product.  Using the above data, We can create a simple product with a name equal to Test Product 1.

You can set price, SKU, visibility, weight, type_id, status, and all the other attributes and custom fields using REST API.

When Run the above script code, You can create a simple product in Magento 2 successfully.