The built-in OpenCartA free open-source CMS for online stores, written in PHP with a MySQL/MariaDB database. Everything that is not part of the core is added through extensions. More in the glossary → API is a set of controllers catalog/ through which you can build a cart, place, edit or delete an order, and add an entry to its history. The core itself uses it when an administrator edits an order. There is no REST API for products, categories or customers in the core. To use the built-in API, you need an API user with a key, an allowed IP address and the correct authentication scheme. And that scheme is completely different in 3.0 and 4.1.
Examples are for OpenCart 4.1; in 4.0.x, model triggers and event handling differ.
What can the built-in API do and what can't it do?
The set of calls revolves around one scenario: assembling an order the same way a customer does at checkout, just without a browser. That is why the API includes the cart, customer, addresses, shipping and payment methods, coupons and order confirmation, but there is no catalog.
| What | OpenCart 3.0 | OpenCart 4.1 |
|---|---|---|
| Login | api/login returns api_token | there is no separate login, each request is signed |
| Cart | api/cart/add, edit, remove, products | api/order with call=product_add, cart |
| Customer | api/customer | call=customer |
| Addresses and methods | api/payment/address, methods, method; likewise api/shipping/... | call=payment_address, shipping_address, shipping_methods, shipping_method, payment_methods, payment_method |
| Discounts | api/coupon, api/voucher, api/reward | call=extension (total modules) |
| Orders | api/order/add, edit, delete, info | call=confirm |
| Order status history | api/order/history | call=history_add |
| Subscriptions | none | api/subscription |
In other words, the built-in API suits tasks like "the CRM changes the order statusA stage of processing: "Pending," "Shipped," "Complete." Custom statuses are added in the dictionary. More in the glossary →" or "a phone operator places an order from another system". You cannot export products, update stock levels or retrieve a list of orders through it: for that, you write a custom endpoint in a module. For how to return JSON from a controllerCode that receives a request and prepares data. More in the glossary →, see the article on AJAX and JSON responses.
One more feature: API calls change the order state through the same models as the storefrontWhat the customer sees: the home page, categories, product page, and cart. More in the glossary →. Adding history goes through addOrderHistory() in 3.0 and addHistory() in 4.1, with all its consequences: stock deduction, emails, coupons. What exactly happens in this case is covered in the article on changing order status.
How do you enable the API in the admin?
In both branches the path is the same: System → Users → API.
- Create an API user or open an existing one (the installer creates one itself). The General tab has the name (API Username), the key (API Key) and the status. The key is generated with a button; its length is sufficient, so do not shorten it.
- Enable the status. A disabled user will pass neither login in 3.0 nor signature verification in 4.1.
- On the IP tab, add the addresses from which requests will come. Without this, access is denied in both versions.
- For order editing in the admin to work, select this user in the API User field on the Option tab in System → Settings.

The third tab differs. In 3.0 it is Sessions: open api_token with IPs, which can be deleted. In 4.1 it is History: a log of calls with the name call and the IP, which startup/api fills in after each successful request. For diagnosing an integration in 4.1, this is the first place to look.
IP address behind a proxy
In 3.0 the IP is taken from REMOTE_ADDR. If the site sits behind Cloudflare or another proxy and the web server does not restore the client's real address, the core sees the proxy's IP. Then you have to whitelist those IPs, which effectively opens the API to everyone. In 4.1 the IP is determined by oc_get_ip(): first CF-Connecting-IP, X-Forwarded-For and other proxy headers, and only then REMOTE_ADDR. This is convenient behind a proxy, but if the site runs without one, a client can set such a header itself. Therefore, in 4.1 the IP whitelist is only a supplementary filter, and real protection comes from the secret key and the signature.
How does authentication work in OpenCart 3.0?
In 3.0 the API is built on sessions. The client first logs in, receives api_token (this is the session identifier) and then passes it as a GET parameter in every request.
catalog/ · OpenCart 3.0.5.1 · lines 10–15
// Login with API Key
if (isset($this->request->post['username']) && isset($this->request->post['key'])) {
$api_info = $this->model_account_api->login($this->request->post['username'], $this->request->post['key']);
} elseif (isset($this->request->post['key'])) {
$api_info = $this->model_account_api->login('Default', $this->request->post['key']);
}
If you send only key, the core looks for a user with the name Default. After checking the key and IP, a new session is created, its id is written to the api_session table, and api_id is stored in the session. After that, startup/session for each api/... route with the api_token parameter looks up this session together with the IP and the user status. Every API controller first checks $this and without it returns an access error.
Example: changing order status through the API (OpenCart 3.0)
# 1. Вхід: у відповіді JSON з api_token
curl -s -X POST "https://example.com/index.php?route=api/login" \
--data-urlencode "username=crm" --data-urlencode "key=ВАШ_КЛЮЧ"
# 2. Історія замовлення 15: order_id у GET, решта в POST
curl -s -X POST "https://example.com/index.php?route=api/order/history&api_token=TOKEN&order_id=15" \
--data "order_status_id=5¬ify=1&comment=Оплата+підтверджена"
API sessions live briefly: startup/session deletes api_session records that have not been accessed for more than an hour. An integration that makes a request once a day must log in again each time. How sessions themselves work is explained in the article on sessions and cache.
How does authentication work in OpenCart 4.1?
In 4.1 the scheme has been rewritten. There is no separate login, and api_token as in 3.0 should not be used here: the 4.1.0.4 database schema has no api_session table. Instead, the startup/api controller verifies every request to api/order and api/subscription. The remaining api/... routes (for example api/cart) are not directly accessible and always return 403.
Required GET parameters: route, call, username, store_id, language, currency, time and signature. It checks that the user exists and is enabled, that the IP is in the whitelist, and that time differs from the server time by no more than 450 seconds. Finally, the HMAC signature is verified.
catalog/ · OpenCart 4.1.0.4 · lines 84–99
if ($status) {
$string = (string)$this->request->get['route'] . "\n";
$string .= (string)$this->request->get['call'] . "\n";
$string .= $api_info['username'] . "\n";
$string .= (string)$this->request->server['HTTP_HOST'] . "\n";
$string .= (!empty($this->request->server['PHP_SELF']) ? rtrim(dirname($this->request->server['PHP_SELF']), '/') . '/' : '/') . "\n";
$string .= (int)$this->request->get['store_id'] . "\n";
$string .= (string)$this->request->get['language'] . "\n";
$string .= (string)$this->request->get['currency'] . "\n";
$string .= md5(http_build_query($this->request->post)) . "\n";
$string .= $time . "\n";
if (!hash_equals(base64_encode(hash_hmac('sha1', $string, $api_info['key'], true)), rawurldecode($this->request->get['signature']))) {
$status = false;
}
}
The client must build exactly the same string. Here is a minimal client for calling history_add that requires no preliminary steps:
Example: signed API call (OpenCart 4.1)
$username = 'crm';
$key = 'ВАШ_КЛЮЧ';
$host = 'example.com';
$path = '/'; // каталог, де лежить index.php, зі слешем у кінці
$time = time();
$get = [
'route' => 'api/order',
'call' => 'history_add',
'username' => $username,
'store_id' => 0,
'language' => 'uk-ua',
'currency' => 'UAH',
'time' => $time
];
$post = [
'order_id' => 15,
'order_status_id' => 5,
'comment' => 'Оплата підтверджена',
'notify' => 1
];
$string = $get['route'] . "\n" . $get['call'] . "\n" . $username . "\n" . $host . "\n" . $path . "\n";
$string .= $get['store_id'] . "\n" . $get['language'] . "\n" . $get['currency'] . "\n";
$string .= md5(http_build_query($post)) . "\n" . $time . "\n";
$get['signature'] = base64_encode(hash_hmac('sha1', $string, $key, true));
$curl = curl_init('https://' . $host . $path . 'index.php?' . http_build_query($get));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
$response = json_decode(curl_exec($curl), true);
Three places where the signature most often doesn't match:
- Host and path. The server takes
HTTP_HOSTand the script directory. If the client callswww.example.combut signsexample.com, the check will fail. For a store in a subdirectory, the path will be/shop/. - POST after processing. The server computes
md5from$this, while Request passes values through->request ->post htmlspecialchars(see the article on Request and Response). As long as the values contain no quotes, ampersands or angle brackets, the hashes match. If they do, the client has to compute the hash from equally escaped values. - Time. A clock difference of more than 450 seconds gives 403 even with the correct key.
How does the admin use the API and why does it matter for modules?
OpenCart 3.0: the order page in the admin opens an API session for the user from config_api_id, passes api_token to the templateA display file: in 2.x it is .tpl, and in 3.0 and 4.1 it is .twig. More in the glossary →, and then JavaScript sends requests from the browser to index.php?route=api/ of the storefront. Because of this, order editing is affected by everything between the browser and the storefront: a different storefront domain, mixed http/https, CORS, WAF.
OpenCart 4.1: the scheme differs from 3.0. There are no api_session and api_token tables here, so the admin does not open an API session the way 3.0 does. The API user for order editing must still be selected in the API User field on the Option tab. For exactly how the admin calls the API controllers, see the admin/ code of your release.
For module authors, two consequences follow:
- Events on the order model (
addOrder,editOrder,addOrderHistoryoraddHistory) fire both when placing an order on the storefront, when an administrator edits an order, and when an external API call is made. If a module needs to distinguish these cases, check the route or$thisin 3.0. How to subscribe to such events is described in the article on the event system.->session ->data['api_id'] - Payment and shipping modules must work correctly without the customer's browser. For example,
getQuote()orgetMethods()must not rely on cookies or session data that does not exist in the API context.
The order data flow is the same as at checkout, so for understanding the fields, the articles on checkout in code and the order table are useful.
When is the built-in API not enough?
In practice, integrations with external services (CRM, accounting systems, marketplaces) almost always need things the built-in API lacks. These include exporting products and stock, a list of new orders from a certain date, and price updates. There are two ways to handle this:
- A custom endpoint in a module. A controller in
catalog/(3.0) orcontroller/ extension/ ... extension/(4.1) that checks its own token or signature and returns JSON. The key check can be modeled on{код}/ catalog/ controller/ ... startup/apior you can use theapitable from the core. The route of such a controller is built by the usual rules (slash in 3.0, dot before the method in 4.1; see the article on routing). - Webhooks. Instead of an external system polling the store, the store itself notifies it about a new order or a status change from an event handler after
addHistory.
The built-in API remains the simplest way to change an order status from outside without custom code: it already performs all the checks, deducts stock and sends emails just like the admin does. For product tasks or bulk operations, it is not suitable.
Summary
- The built-in API handles orders: cart, customer, addresses, shipping and payment methods, confirmation and history. It has no catalog.
- The user, key and IP whitelist are set in System → Users → API. For the admin, select it in the API User field on the Option tab.
- OpenCart 3.0:
api/loginreturnsapi_token, which lives for an hour since the last access and is verified together with the IP. - OpenCart 4.1: there is no login; only
api/orderandapi/subscriptionare available, and each request is signed with HMAC-SHA1 according to the rules ofstartup/api.phpwith a 450-second window. - The 3.0 admin calls the API from the browser through
api_token, while in 4.1 there are no API sessions. Events on the order model fire in all these scenarios. - For products, stock and exports, write a custom endpoint or webhooks.
Other materials on data exchange with external systems are collected in the section "API and integrations".