Installation and consent (OAuth)
The API uses OAuth 2.0, authorization code with PKCE. Your app sees no data before the store owner approves.
Install and consent
- The merchant presses «Install» in the app store and lands on your app's install address with the store id.
- Your app creates a random verifier (code_verifier) and sends the merchant to the consent page with its challenge.
- The store owner reads the permissions with your reasons and your app's terms, and approves or declines. A paid app is charged here.
- We send the merchant back to your registered return address with a code and the same state.
- Your app's server exchanges the code for an access token and a refresh token, sending the secret and the verifier.
The consent screen
At install the store owner sees one screen. Nothing is shared with your app before they accept.
- Your app's name, its developer, and its price.
- Each scope on its own line with the reason you gave, and the sensitive ones marked.
- For each action scope, how your app may act: the merchant chooses.
- Your app's terms and Naqllah's terms for third-party apps.
- Two buttons: accept or decline. A paid app is charged on acceptance.
When the merchant accepts
We send the merchant back to your return address with code and the same state.
Location: https://your.app/callback?code=nqc_…&state=STATE
When the merchant declines
We send the merchant back to your return address with error=access_denied and the same state. Show a clear page; do not start the flow again on your own.
Location: https://your.app/callback?error=access_denied&state=STATE
How an action runs
For a scope that does something in the store, such as requesting a waybill, the merchant chooses one of these modes at install and can change it later. A request whose conditions do not hold is refused.
| Mode | Meaning |
|---|---|
auto | Runs at once. |
notify | Runs at once, and the merchant is told. This is the default. |
conditions | Runs only when the merchant's conditions hold; otherwise it is refused with status refused. |
The authorize address
| Name | Description |
|---|---|
response_typerequired | Always code. |
client_idrequired | Your app id (app_…). |
redirect_urirequired | One of your registered return addresses, exactly. |
scoperequired | The scopes you ask for, separated by spaces. Only scopes registered for your app. |
staterequired | A random value you keep and compare when the merchant comes back. |
code_challengerequired | BASE64URL(SHA256(code_verifier)). |
code_challenge_methodrequired | Always S256. |
GET https://naqllah.com/dashboard/apps/authorize
?response_type=code
&client_id=app_…
&redirect_uri=https://your.app/callback
&scope=store.read%20orders.read%20webhooks.read
&state=STATE
&code_challenge=CHALLENGE
&code_challenge_method=S256
The challenge is required with method S256, and state is required: compare it with what you sent before accepting the code.
Creating the verifier and the challenge
VERIFIER=$(openssl rand -base64 48 | tr -d '=+/\n' | cut -c1-64)
CHALLENGE=$(printf '%s' "$VERIFIER" | openssl dgst -sha256 -binary | openssl base64 | tr '+/' '-_' | tr -d '=\n')
STATE=$(openssl rand -hex 16)
<?php
$verifier = rtrim(strtr(base64_encode(random_bytes(48)), '+/', '-_'), '=');
$challenge = rtrim(strtr(base64_encode(hash('sha256', $verifier, true)), '+/', '-_'), '=');
$state = bin2hex(random_bytes(16));
// keep both until the merchant comes back
$_SESSION['pkce_verifier'] = $verifier;
$_SESSION['oauth_state'] = $state;
import crypto from 'node:crypto';
const verifier = crypto.randomBytes(48).toString('base64url');
const challenge = crypto.createHash('sha256').update(verifier).digest('base64url');
const state = crypto.randomBytes(16).toString('hex');
// keep both until the merchant comes back
req.session.pkceVerifier = verifier;
req.session.oauthState = state;
import base64, hashlib, secrets
verifier = secrets.token_urlsafe(48)
challenge = base64.urlsafe_b64encode(
hashlib.sha256(verifier.encode()).digest()
).rstrip(b"=").decode()
state = secrets.token_hex(16)
# keep both until the merchant comes back
session["pkce_verifier"] = verifier
session["oauth_state"] = state
Handling the return
At your return address: compare the state with the one you kept, then exchange the code from your server with the secret and the verifier. A code works once and expires within minutes.
curl -X POST "https://naqllah.com/oauth/token" \
-u "$NAQLLAH_CLIENT_ID:$NAQLLAH_CLIENT_SECRET" \
-d grant_type=authorization_code \
-d code="$CODE" \
-d redirect_uri=https://your.app/callback \
-d code_verifier="$CODE_VERIFIER"
<?php
if (! hash_equals($_SESSION['oauth_state'] ?? '', $_GET['state'] ?? '')) {
http_response_code(400);
exit;
}
$ch = curl_init('https://naqllah.com/oauth/token');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERPWD => getenv('NAQLLAH_CLIENT_ID') . ':' . getenv('NAQLLAH_CLIENT_SECRET'),
CURLOPT_POSTFIELDS => http_build_query([
'grant_type' => 'authorization_code',
'code' => $_GET['code'],
'redirect_uri' => 'https://your.app/callback',
'code_verifier' => $_SESSION['pkce_verifier'],
]),
]);
$token = json_decode(curl_exec($ch), true);
// $token['access_token'], $token['refresh_token'], $token['expires_in']
if (req.query.state !== req.session.oauthState) return res.sendStatus(400);
const code = req.query.code;
const codeVerifier = req.session.pkceVerifier;
const basic = Buffer.from(
`${process.env.NAQLLAH_CLIENT_ID}:${process.env.NAQLLAH_CLIENT_SECRET}`,
).toString('base64');
const res = await fetch('https://naqllah.com/oauth/token', {
method: 'POST',
headers: { Authorization: `Basic ${basic}` },
body: new URLSearchParams({
grant_type: 'authorization_code',
code: code,
redirect_uri: 'https://your.app/callback',
code_verifier: codeVerifier,
}),
});
const token = await res.json();
import os, secrets
import requests
if not secrets.compare_digest(session.get("oauth_state", ""), request.args.get("state", "")):
abort(400)
code = request.args["code"]
code_verifier = session["pkce_verifier"]
res = requests.post(
"https://naqllah.com/oauth/token",
auth=(os.environ["NAQLLAH_CLIENT_ID"], os.environ["NAQLLAH_CLIENT_SECRET"]),
data={
"grant_type": "authorization_code",
"code": code,
"redirect_uri": "https://your.app/callback",
"code_verifier": code_verifier,
},
timeout=10,
)
token = res.json()
Authentication errors
Consent page errors come back to your return address in error, with state; token request errors come in the response body.
| HTTP | Code | Meaning |
|---|---|---|
| 302 | access_denied | The store owner declined the install. |
| 400 | invalid_request | A missing or wrong parameter, such as no PKCE. |
| 400 | invalid_scope | An unknown scope, or one not granted to your app. |
| 400 | invalid_redirect_uri | The return address does not match a registered one exactly. |
| 400 | unauthorized_client | The app may not make this request now (for example, it is suspended). |
| 400 | unsupported_response_type | response_type is not code. |
| 400 | unsupported_grant_type | grant_type is not authorization_code or refresh_token. |
| 400 | invalid_grant | The code is expired or used, or does not match the verifier or the return address. |
| 401 | invalid_client | The app id or secret is wrong. |
Uninstalling
When the merchant uninstalls your app, every token of that install stops at once and you receive app.uninstalled. Delete what you kept from that store.
Version v1.0.0 Changelog Service status openapi-v1.json Support