نقلة
Docs menuInstallation and consent (OAuth)

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

  1. The merchant presses «Install» in the app store and lands on your app's install address with the store id.
  2. Your app creates a random verifier (code_verifier) and sends the merchant to the consent page with its challenge.
  3. The store owner reads the permissions with your reasons and your app's terms, and approves or declines. A paid app is charged here.
  4. We send the merchant back to your registered return address with a code and the same state.
  5. Your app's server exchanges the code for an access token and a refresh token, sending the secret and the verifier.

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.

302 · accepted
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.

302 · declined
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.

ModeMeaning
autoRuns at once.
notifyRuns at once, and the merchant is told. This is the default.
conditionsRuns only when the merchant's conditions hold; otherwise it is refused with status refused.

The authorize address

NameDescription
response_typerequiredAlways code.
client_idrequiredYour app id (app_…).
redirect_urirequiredOne of your registered return addresses, exactly.
scoperequiredThe scopes you ask for, separated by spaces. Only scopes registered for your app.
staterequiredA random value you keep and compare when the merchant comes back.
code_challengerequiredBASE64URL(SHA256(code_verifier)).
code_challenge_methodrequiredAlways S256.
GET · authorize
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

shell · openssl
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
<?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;
Node.js
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;
Python
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
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
<?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']
Node.js
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();
Python
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.

HTTPCodeMeaning
302access_deniedThe store owner declined the install.
400invalid_requestA missing or wrong parameter, such as no PKCE.
400invalid_scopeAn unknown scope, or one not granted to your app.
400invalid_redirect_uriThe return address does not match a registered one exactly.
400unauthorized_clientThe app may not make this request now (for example, it is suspended).
400unsupported_response_typeresponse_type is not code.
400unsupported_grant_typegrant_type is not authorization_code or refresh_token.
400invalid_grantThe code is expired or used, or does not match the verifier or the return address.
401invalid_clientThe 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