Python backend integration: FastAPI & Django
Call KhaleejiAPI from your Python server and expose only the specific operations your signed-in users need. The API key stays in a server secret; the browser talks only to your application.
1. Create a key and authenticate
- Create an API key in your KhaleejiAPI dashboard and grant only the scopes required by your server routes.
- Store it as
KHALEEJI_API_KEYin your local environment file (ignored by Git) or production secret manager. - Send the key on each server-to-server API call in
Authorization:Bearer <key>. - Never accept an upstream API key from the browser, forward browser Authorization headers, or include the key in an error response.
Required scopes for the examples below: validation:emirates-id, validation:saudi-id, validation:vat, finance:zakat, finance:sukuk, and finance:takaful. Grant only what the app actually uses.
# Set this in your local environment or secret manager, never in browser code.export KHALEEJI_API_KEY="your_api_key_here"export KHALEEJI_API_BASE_URL="https://khaleejiapi.dev/api/v1" # Verify the key and the validation:emirates-id scope with a synthetic test ID.curl --get "$KHALEEJI_API_BASE_URL/emirates-id/validate" \ --header "$(printf '%s: %s %s' Authorization Bearer "$KHALEEJI_API_KEY")" \ --data-urlencode "id=$SYNTHETIC_EMIRATES_ID"Both framework examples load the same server-only configuration with Pydantic Settings. Environment variables take precedence over the optional local .env file; keep that file out of Git and use your deployment's secret manager in production.
python -m pip install "pydantic-settings[dotenv]" httpx# myproject/config.pyfrom pydantic import Field, SecretStrfrom pydantic_settings import BaseSettings, SettingsConfigDict class Settings(BaseSettings): model_config = SettingsConfigDict( env_file=".env", env_file_encoding="utf-8", extra="ignore", ) khaleeji_api_key: SecretStr = Field(min_length=1) khaleeji_api_base_url: str = "https://khaleejiapi.dev/api/v1" settings = Settings()Both proxies share this English/Arabic negotiator for generated errors. It honors Accept-Language quality weights, excludes q=0 matches, and uses header order to break ties. Explicit language preferences override wildcards. Invalid weights are ignored; if no supported language is acceptable, these examples fall back to English.
# myproject/localization.pydef preferred_language(header: str) -> str: preferences = [] for position, item in enumerate(header.split(",")): parts = [part.strip().lower() for part in item.split(";")] quality = 1.0 try: for parameter in parts[1:]: name, separator, value = parameter.partition("=") if name.strip() == "q": quality = float(value) if separator else -1 except ValueError: continue if 0 <= quality <= 1: preferences.append((parts[0], quality, -position)) candidates = [] for language in ("en", "ar"): matches = [ (0 if tag == "*" else 1, quality, order) for tag, quality, order in preferences if tag == "*" or tag.split("-", 1)[0] == language ] if matches: _, quality, order = max(matches) if quality > 0: candidates.append((quality, order, language)) return max(candidates)[2] if candidates else "en"2. FastAPI proxy
Install FastAPI, then use a lifespan-managed httpx.AsyncClient for connection pooling. The proxy uses fixed upstream paths, the Pydantic Settings above, and your application's auth dependency. Connect require_authenticated_user to your own login system and per-user rate limiter before exposing these routes.
python -m pip install fastapi uvicorn# main.pyimport loggingfrom contextlib import asynccontextmanagerfrom typing import Literal import httpxfrom fastapi import Depends, FastAPI, Query, Request, Responsefrom fastapi.responses import JSONResponsefrom pydantic import BaseModelfrom starlette.middleware.cors import CORSMiddleware from myproject.config import settingsfrom myproject.localization import preferred_languagefrom myproject.auth import require_authenticated_user # Use your application's auth dependency logger = logging.getLogger(__name__)RATE_LIMIT_HEADERS = ("Retry-After", "X-RateLimit-Limit", "X-RateLimit-Remaining", "X-RateLimit-Reset") @asynccontextmanagerasync def lifespan(app: FastAPI): app.state.khaleeji = httpx.AsyncClient(timeout=httpx.Timeout(10.0)) yield await app.state.khaleeji.aclose() app = FastAPI(lifespan=lifespan) # Only needed when your frontend is on a different origin. Use exact trusted origins.app.add_middleware( CORSMiddleware, allow_origins=["https://app.example.com"], allow_credentials=True, allow_methods=["GET", "POST"], allow_headers=["Content-Type", "X-CSRFToken"], expose_headers=["Retry-After", "X-RateLimit-Limit", "X-RateLimit-Remaining", "X-RateLimit-Reset"],) async def forward(request: Request, method: str, endpoint: str, *, params=None, payload=None) -> Response: try: upstream = await request.app.state.khaleeji.request( method, f"{settings.khaleeji_api_base_url.rstrip('/')}/{endpoint}", headers={ "Authorization": "Bearer " + settings.khaleeji_api_key.get_secret_value(), "Accept": "application/json", "Accept-Language": request.headers.get("accept-language", "en"), }, params=params, json=payload, ) except httpx.TimeoutException: return localized_error(request, 504, "UPSTREAM_TIMEOUT", "Request timed out", "انتهت مهلة الطلب") except httpx.RequestError: return localized_error(request, 502, "UPSTREAM_UNAVAILABLE", "Service is temporarily unavailable", "الخدمة غير متاحة مؤقتًا") if upstream.status_code in (401, 403): logger.error("KhaleejiAPI rejected the server credential (status=%s)", upstream.status_code) return localized_error( request, 502, "UPSTREAM_AUTH_CONFIGURATION", "Check the server API key and its endpoint scopes", "تحقق من مفتاح API ونطاقات نقطة النهاية", ) if upstream.status_code >= 500: logger.warning("KhaleejiAPI returned an upstream error (status=%s)", upstream.status_code) return localized_error(request, 502, "UPSTREAM_UNAVAILABLE", "Service is temporarily unavailable", "الخدمة غير متاحة مؤقتًا") headers = {"Content-Type": upstream.headers.get("Content-Type", "application/json")} headers.update({name: upstream.headers[name] for name in RATE_LIMIT_HEADERS if name in upstream.headers}) return Response(content=upstream.content, status_code=upstream.status_code, headers=headers) def localized_error(request: Request, status_code: int, code: str, english: str, arabic: str) -> JSONResponse: language = preferred_language(request.headers.get("accept-language", "")) return JSONResponse( status_code=status_code, content={"error": {"code": code, "message": arabic if language == "ar" else english}}, ) class VATRequest(BaseModel): tin: str country: Literal["AE", "SA", "BH", "OM"] class ZakatRequest(BaseModel): currency: str = "AED" assets: list[dict[str, object]] @app.get("/api/khaleeji/emirates-id", dependencies=[Depends(require_authenticated_user)])async def validate_emirates_id(request: Request, id: str): return await forward(request, "GET", "validation/emirates-id", params={"id": id}) @app.get("/api/khaleeji/saudi-id", dependencies=[Depends(require_authenticated_user)])async def validate_saudi_id(request: Request, id: str): return await forward(request, "GET", "validation/saudi-id", params={"id": id}) @app.post("/api/khaleeji/vat", dependencies=[Depends(require_authenticated_user)])async def validate_vat(request: Request, payload: VATRequest): return await forward(request, "POST", "vat/validate", payload=payload.model_dump()) @app.post("/api/khaleeji/zakat", dependencies=[Depends(require_authenticated_user)])async def calculate_zakat(request: Request, payload: ZakatRequest): return await forward(request, "POST", "zakat/calculate", payload=payload.model_dump()) @app.get("/api/khaleeji/sukuk", dependencies=[Depends(require_authenticated_user)])async def list_sukuk(request: Request, country: str | None = None): params = {"country": country} if country else None return await forward(request, "GET", "sukuk/list", params=params) @app.get("/api/khaleeji/takaful", dependencies=[Depends(require_authenticated_user)])async def estimate_takaful( request: Request, takaful_type: str = Query(alias="type"), coverage: float = Query(gt=0), currency: str = "AED", country: str = "AE", model: str = "wakalah_mudarabah",): params = { "type": takaful_type, "coverage": coverage, "currency": currency, "country": country, "model": model, } return await forward(request, "GET", "takaful/calculate", params=params)3. Django proxy
These Django 5+ async views use request.auser() andhttpx.AsyncClient for non-blocking upstream calls. Keep Django's authentication middleware and CsrfViewMiddleware enabled for browser-facing POST requests; do not add csrf_exempt. Also apply your app's per-user throttling and authorization rules.
python -m pip install "django>=5.0"# views.pyimport jsonimport logging import httpxfrom django.http import HttpResponse, JsonResponsefrom django.views.decorators.http import require_GET, require_POST from myproject.config import settingsfrom myproject.localization import preferred_language logger = logging.getLogger(__name__)RATE_LIMIT_HEADERS = ("Retry-After", "X-RateLimit-Limit", "X-RateLimit-Remaining", "X-RateLimit-Reset") def localized_error(request, status, code, english, arabic): language = preferred_language(request.headers.get("Accept-Language", "")) message = arabic if language == "ar" else english return JsonResponse({"error": {"code": code, "message": message}}, status=status) async def forward(request, method, endpoint, *, params=None, payload=None): user = await request.auser() if not user.is_authenticated: return localized_error(request, 401, "UNAUTHENTICATED", "Sign in first", "يرجى تسجيل الدخول") try: async with httpx.AsyncClient(timeout=httpx.Timeout(10.0), follow_redirects=False) as client: upstream = await client.request( method, f"{settings.khaleeji_api_base_url.rstrip('/')}/{endpoint}", headers={ "Authorization": "Bearer " + settings.khaleeji_api_key.get_secret_value(), "Accept": "application/json", "Accept-Language": request.headers.get("Accept-Language", "en"), }, params=params, json=payload, ) except httpx.TimeoutException: return localized_error(request, 504, "UPSTREAM_TIMEOUT", "Request timed out", "انتهت مهلة الطلب") except httpx.RequestError: logger.error("Could not reach KhaleejiAPI") return localized_error(request, 502, "UPSTREAM_UNAVAILABLE", "Service is temporarily unavailable", "الخدمة غير متاحة مؤقتًا") if upstream.status_code in (401, 403): logger.error("KhaleejiAPI rejected the server credential (status=%s)", upstream.status_code) return localized_error( request, 502, "UPSTREAM_AUTH_CONFIGURATION", "Check the server API key and its endpoint scopes", "تحقق من مفتاح API ونطاقات نقطة النهاية", ) if upstream.status_code >= 500: logger.warning("KhaleejiAPI returned an upstream error (status=%s)", upstream.status_code) return localized_error(request, 502, "UPSTREAM_UNAVAILABLE", "Service is temporarily unavailable", "الخدمة غير متاحة مؤقتًا") response = HttpResponse( upstream.content, status=upstream.status_code, content_type=upstream.headers.get("Content-Type", "application/json"), ) for name in RATE_LIMIT_HEADERS: if name in upstream.headers: response[name] = upstream.headers[name] return response def read_json_body(request): try: payload = json.loads(request.body) except (UnicodeDecodeError, json.JSONDecodeError): return None return payload if isinstance(payload, dict) else None @require_GETasync def validate_emirates_id(request): return await forward(request, "GET", "validation/emirates-id", params={"id": request.GET.get("id", "")}) @require_GETasync def validate_saudi_id(request): return await forward(request, "GET", "validation/saudi-id", params={"id": request.GET.get("id", "")}) @require_POSTasync def validate_vat(request): payload = read_json_body(request) if payload is None: return localized_error(request, 400, "INVALID_JSON", "Expected a JSON object", "يجب إرسال كائن JSON") return await forward(request, "POST", "vat/validate", payload=payload) @require_POSTasync def calculate_zakat(request): payload = read_json_body(request) if payload is None: return localized_error(request, 400, "INVALID_JSON", "Expected a JSON object", "يجب إرسال كائن JSON") return await forward(request, "POST", "zakat/calculate", payload=payload) @require_GETasync def list_sukuk(request): params = {"country": request.GET["country"]} if request.GET.get("country") else None return await forward(request, "GET", "sukuk/list", params=params) @require_GETasync def estimate_takaful(request): allowed = ("type", "coverage", "currency", "country", "model") params = {name: request.GET[name] for name in allowed if name in request.GET} return await forward(request, "GET", "takaful/calculate", params=params) Add these explicit paths in your app's URL configuration; avoid forwarding arbitrary caller-supplied paths.
# urls.py — use explicit routes; do not expose a caller-controlled catch-all proxy.from django.urls import pathfrom . import views urlpatterns = [ path("api/khaleeji/emirates-id", views.validate_emirates_id), path("api/khaleeji/saudi-id", views.validate_saudi_id), path("api/khaleeji/vat", views.validate_vat), path("api/khaleeji/zakat", views.calculate_zakat), path("api/khaleeji/sukuk", views.list_sukuk), path("api/khaleeji/takaful", views.estimate_takaful),]4. Call your backend and handle errors
The browser calls your application's same-origin route—not KhaleejiAPI directly—and sends only its normal app session and framework CSRF token. Upstream 400 responses and 429 rate-limit responses retain their response body and status; only the selected rate-limit headers are copied. The proxy forwards the browser'sAccept-Language preference upstream and returns its own timeout, authentication-configuration, and availability errors in English or Arabic. Keep machine-readable error code values stable so clients can branch on them regardless of locale.
// Browser code calls your authenticated application, never KhaleejiAPI directly.const response = await fetch("/api/khaleeji/vat", { method: "POST", credentials: "same-origin", headers: { "Content-Type": "application/json", "X-CSRFToken": csrfToken, // Include your framework's CSRF token for session-authenticated POSTs. }, body: JSON.stringify({ tin: "your-test-number", country: "AE" }),}) if (response.status === 429) { const retryAfter = response.headers.get("Retry-After") // Show a retry message; do not retry in a tight loop.}const result = await response.json()| Upstream status | Meaning and action | Proxy response |
|---|---|---|
401 Unauthorized | The server key is missing, invalid, or not being sent as a ****** Check the backend secret and authorization header; do not retry unchanged. | Map to a sanitized 502; log the status only, never the key. |
403 Forbidden | The key does not have the scope required by that endpoint. Add only the required scope in the dashboard. | Map to the same sanitized 502 configuration error. |
429 Too Many Requests | The key reached a rate or plan limit. Respect Retry-After; back off with jitter and avoid tight retry loops. | Preserve status, API error body, and available rate-limit headers. |
5xx or timeout | KhaleejiAPI or the network is temporarily unavailable. Retry only when appropriate for the operation. | Return a sanitized 502 or 504; do not expose upstream internals. |
5. Endpoint examples and request shapes
These examples use the implemented service-first API paths. The validation endpoints accept an id query parameter; VAT and Zakat accept JSON POST bodies; Sukuk and Takaful use GET query parameters.
| Use case | KhaleejiAPI request | Example input |
|---|---|---|
| Emirates ID | GET /api/v1/emirates-id/validate?id=... | id=784-… |
| Saudi National ID / Iqama | GET /api/v1/saudi-id/validate?id=... | id=1XXXXXXXXX or 2XXXXXXXXX |
| GCC VAT | POST /api/v1/vat/validate | {"tin":"…","country":"AE"} |
| Zakat | POST /api/v1/zakat/calculate | {"currency":"AED","assets":[{"type":"cash","value":50000}]} |
| Sukuk reference data | GET /api/v1/sukuk/list?country=AE | country=AE (optional) |
| Takaful estimate | GET /api/v1/takaful/calculate?type=health&coverage=100000&country=AE | type, coverage, currency, country, model |
Emirates ID, Saudi ID, and VAT checks validate format/check digits; they are not live identity or tax-registration lookups. Sukuk is informational reference data. Takaful results are indicative estimates, not binding operator quotes.