Add Product
Adds a new product to your store. A successful request returns the ID of the created product.
Request Body Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
name | string | Yes | Product name (max 128 characters) |
stock_code | string | Yes | Unique stock keeping unit |
cost | float | No | Cost Price |
price | float | Yes | Sale price (tax included) |
stock | integer | Yes | Stock quantity |
category | string | Yes | Category Name - If not, add it |
brand | string | Yes | Brand Name - If not, add it |
description_html | string | No | Product description (HTML supported) |
barcode | string | No | Product barcode |
active | integer | Yes | 1 or 0 |
images | array | No | List of image URLs |
Note: Sending a request with the same
stock_code value will return a
error.
PHP
<?php
$baseUrl = 'https://example.com/api/v1/';
$apiKey = ''; // You need to obtain a token from the admin for the API Key.
$result = '';
function apiRequest($method, $url, $apiKey, $data = null)
{
$ch = curl_init();
$headers = [
'X-API-Key: ' . trim($apiKey),
'Accept: application/json',
];
if ($data !== null) {
$headers[] = 'Content-Type: application/json';
}
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_SSL_VERIFYPEER => false,
]);
if ($data !== null) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data, JSON_UNESCAPED_UNICODE));
}
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
return [
'http_code' => $httpCode,
'curl_error' => $error,
'response' => json_decode($response, true) ?: $response,
];
}
function apiUploadImage($url, $apiKey, $filePath)
{
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
'X-API-Key: ' . trim($apiKey),
'Accept: application/json',
],
CURLOPT_POSTFIELDS => [
'image' => new CURLFile($filePath, mime_content_type($filePath) ?: 'image/jpeg', basename($filePath)),
],
CURLOPT_SSL_VERIFYPEER => false,
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return [
'http_code' => $httpCode,
'response' => json_decode($response, true) ?: $response,
];
}
// Add New Product Multi Lang
$payload = [
"translations" => [
"tr" => [
"name" => "Pamuklu Tişört",
"slug" => "pamuklu-tisort",
"description_html" => "<p>Yumuşak kumaş</p>"
],
"en" => [
"name" => "Cotton T-Shirt",
"slug" => "cotton-t-shirt",
"description_html" => "<p>Soft fabric</p>"
]
],
'category' => "Test Category",
'brand' => "Test Brand",
'cost' => 80,
'price' => 199.99,
'stock' => 15,
'active' => 1,
'barcode' => "86991233322",
'stock_code' => "fs123",
"variations" => [
[
"sku" => "TSH-RED-M",
"barcode" => "8690000001001",
"options" => [ "Color" => "Red", "Size" => "M" ],
"price" => 249.90, // OR NULL
"stock" => 12,
"active" => true
],
],
];
$result = apiRequest('POST', $baseUrl . '/products', $apiKey, $payload);
// Upload Image
$id = (int) $productID; //First, add the product, then get the product ID number and enter it here.
if ($id <= 0) {
$result = ['http_code' => 0, 'response' => 'Please send product id'];
} elseif (!empty($_FILES['image']['tmp_name'])) {
$result = apiUploadImage($baseUrl . '/products/' . $id . '/image', $apiKey, $_FILES['image']['tmp_name']);
} elseif (trim($_POST['image_url'] ?? '') !== '') {
$result = apiRequest('POST', $baseUrl . '/products/' . $id . '/image', $apiKey, [
'image_url' => trim($_POST['image_url']),
]);
} else {
$result = ['http_code' => 0, 'response' => 'invalid image url'];
}
Sample Response (JSON)
(
[http_code] => 201
[curl_error] =>
[response] => Array
(
[success] => 1
[message] => Product added
[data] => Array
(
[id] => 6
[name] => Test Product Name
[slug] => test-product-name
[url] => http://localhost/frisay/test-category/test-product-name-6
[price] => 199.99
[old_price] => 0
[stock] => 15
[in_stock] => 1
[active] => 1
[barcode] => 86991233322
[stock_code] => fs123
[id_category] => 5
[category_name] => Test Category
[id_brand] => 4
[brand_name] => Test Brand
[image_url] => http://localhost/frisay/templates/default/img/favicon.png
[short_description] =>
[description] =>
[description_html] =>
[meta_title] =>
[meta_description] =>
[vat] => 20
[doviz] => try
[doviz_price] => 199.99
[doviz_old_price] => 0
[cargo_day] => 0
[label] =>
[product_video] =>
[desi] => 0
[images] => Array
(
)
)
)
)
// Image Upload
Array
(
[http_code] => 201
[curl_error] =>
[response] => Array
(
[success] => 1
[message] => Image uploaded
[data] => Array
(
[id] => 1
[product_id] => 6
[url] => http://localhost/frisay/img/products/1.jpg
)
)
)