Unofficial REST API for ZATCA (Saudi e-invoicing): compliance/production CSID, simplified and standard invoice reporting and clearance.
- PHP 8.4+ (extensions:
curl,dom,json,mbstring,openssl,pdo,redis,xml,xmlwriter,zip,gmp) - Composer
- Redis (optional; can use file cache via
.env)
-
Clone and go to the project
cd zatca-lib -
Environment
cp .env.example .env
Edit
.envif you need to change ports or secrets. Defaults are fine for local use. -
Run
docker compose up -d
-
Install dependencies (inside container)
If you change code and need to run composer:docker compose exec web composer install -
Base URL
- API:
http://localhost:8080/api/v1 - Health:
http://localhost:8080/api/v1/health - Mail UI (MailHog):
http://localhost:8025
- API:
-
Clone and enter project
git clone https://github.com/mudassaralichouhan/zatca-xml-php zatca-lib && cd zatca-lib
-
Install PHP dependencies
composer install --no-dev
-
Environment
cp .env.example .env
Set at least:
BASE_URL– your public URL (e.g.https://api.yourdomain.com)APP_ENV=proJWT_SECRETandCRYPTO_SECRET_KEY– strong random valuesREDIS_*if using Redis, or keepSTORAGE_DRIVER=fileandSTORAGE_FILE_PATH=storage/cache- Mail settings if you use email (e.g. confirmations)
-
Permissions
chmod -R 775 storage chown -R www-data:www-data storage # or your web server user -
Web server
- Document root: project root (where
index.phpis), or point your vhost to this directory. - Ensure
index.phpreceives all requests (e.g. nginxtry_files $uri $uri/ /index.php?$query_string;).
- Document root: project root (where
-
Cron (optional)
If you use the cleanup cron, add something like:* * * * * cd /path/to/zatca-lib && php -r "require 'vendor/autoload.php'; /* your cleanup script */"
-
Import the collection from: https://gist.github.com/mudassaralichouhan/1f075942c7d9dcf2a372cc1ffa10b5d9
-
Variables
base_url=http://localhost:8080/api/v1(Docker) orhttps://your-api-domain.com/api/v1(server).bearer_token= set after login (e.g. from auth → login response).x_api_key= your API key (from auth → key after login).
-
Headers
- zatca-mode:
developer-portalorsimulation(as in the collection). - Authorization: Bearer token for auth endpoints; X-API-KEY for Fatoora endpoints (CSID, reporting, clearance).
- zatca-mode:
-
Flow
- auth → register → confirm email (or use auth → mail/resend).
- auth → login → copy
access_tokenintobearer_token. - auth → key (PATCH) to add allowed IPs/domains; use the returned API key as
x_api_key. - Call fatoora → csr (compliance/production) and reporting / clearance with
request_idand the invoice payload.
| Area | Method | Path | Description |
|---|---|---|---|
| Health | GET | /api/v1/health |
Health check |
| Auth | POST | /api/v1/auth/register |
Register |
| Auth | POST | /api/v1/auth/login |
Login (get JWT) |
| Auth | GET | /api/v1/auth/me |
Current user (JWT) |
| Auth | GET/PATCH/PUT/DELETE | /api/v1/auth/key |
API keys (IP/domain) |
| CSID | POST/GET | /api/v1/csid/compliance |
Compliance CSID |
| CSID | GET | /api/v1/csid/production |
Production CSID |
| Reporting | POST | /api/v1/reporting?request_id=... |
Simplified invoices |
| Clearance | POST | /api/v1/clearance?request_id=... |
Standard invoices |
Use the Postman collection for exact request bodies and examples.
Contributions are welcome! Feel free to open issues or submit pull requests on the repository.