惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
H
Help Net Security
云风的 BLOG
云风的 BLOG
Apple Machine Learning Research
Apple Machine Learning Research
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
D
Docker
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Blog — PlanetScale
Blog — PlanetScale
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
GbyAI
GbyAI
博客园 - Franky
B
Blog RSS Feed
Stack Overflow Blog
Stack Overflow Blog
L
LangChain Blog
量子位
V
Visual Studio Blog
Y
Y Combinator Blog
小众软件
小众软件
N
Netflix TechBlog - Medium
博客园 - 三生石上(FineUI控件)
Microsoft Security Blog
Microsoft Security Blog
雷峰网
雷峰网

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Integrating Pakistani Businesses with the FBR Digital Inv...
Waseem ur Re · 2026-05-17 · via DEV Community

If you build or maintain software for Pakistani businesses, FBR Digital Invoicing integration is now in your backlog whether you wanted it or not. Under SRO 709(I)/2025, every sales-tax-registered person in Pakistan must transmit invoices to FBR in real time via the Digital Invoicing API operated by PRAL (Pakistan Revenue Automation).

This post is the practical developer walkthrough I wish existed when I first built integrations against this API. We'll cover the architecture, the auth flow, the JSON payload, sandbox testing for the 28 mandatory scenarios, retry logic for FBR's outages, and the going-to-production checklist.

The architecture in 30 seconds

Your app → PRAL Digital Invoicing API → FBR core system
              ↓
        IRN + QR code returned to you
              ↓
        Print on the customer's invoice

Enter fullscreen mode Exit fullscreen mode

Three endpoints matter:

  • validateinvoicedata — pre-flight check before live submission
  • postinvoicedata — actual invoice submission, returns IRN + QR code
  • cancelinvoicedata — cancellation within the 72-hour correction window (post April 2026)

You authenticate with a bearer token issued via the FBR IRIS portal per registered NTN.

Step 1: Get your bearer token

  1. Log in to iris.fbr.gov.pk with the business's CNIC/NTN
  2. Navigate to Digital InvoicingIntegration Mode
  3. Select "Proceed with PRAL as Licensed Integrator"
  4. The portal generates a bearer token tied to that NTN

The token is opaque and long-lived (it doesn't expire frequently). Store it encrypted — under no circumstances commit it to source control or log it in plain text. AES-256 at rest is the minimum standard.

Step 2: Construct the invoice payload

FBR expects a strict JSON structure. Here's a minimal but complete payload for a B2B taxable supply:

{
  "invoiceType": "Sale Invoice",
  "invoiceDate": "2026-05-16",
  "sellerNTNCNIC": "1234567",
  "sellerBusinessName": "Acme Trading (Pvt) Ltd",
  "sellerProvince": "Punjab",
  "sellerAddress": "Plot 12, Lahore",
  "buyerNTNCNIC": "7654321",
  "buyerBusinessName": "Beta Industries (Pvt) Ltd",
  "buyerProvince": "Sindh",
  "buyerAddress": "Industrial Area, Karachi",
  "buyerRegistrationType": "Registered",
  "invoiceRefNo": "ACM-2026-0001",
  "items": [
    {
      "hsCode": "1234.5678",
      "productDescription": "Industrial pump model X-200",
      "rate": "18%",
      "uoM": "Numbers, pieces, units",
      "quantity": 5,
      "totalValues": 50000,
      "valueSalesExcludingST": 42372.88,
      "fixedNotifiedValueOrRetailPrice": 0,
      "salesTaxApplicable": 7627.12,
      "salesTaxWithheldAtSource": 0,
      "extraTax": 0,
      "furtherTax": 0,
      "sroScheduleNo": "",
      "fedPayable": 0,
      "discount": 0,
      "saleType": "Goods at Standard Rate (default)",
      "sroItemSerialNo": ""
    }
  ]
}

Enter fullscreen mode Exit fullscreen mode

Critical fields to get right:

  • sellerNTNCNIC and buyerNTNCNIC — exactly as registered with FBR; mismatches return validation errors
  • hsCode — must match the actual product category (FBR validates against their HS Code database)
  • rate — string format with % sign, not a number
  • saleType — one of FBR's predefined values (e.g., "Goods at Standard Rate (default)", "3rd Schedule Goods", "Steel Melting and Re-Rolling")
  • valueSalesExcludingST and salesTaxApplicable — must mathematically reconcile to totalValues

If your software can't render these from your existing invoice data model, you have an integration bug, not an FBR problem.

Step 3: Submit the invoice

curl -X POST 'https://gw.fbr.gov.pk/di_data/v1/di/postinvoicedata' \
  -H 'Authorization: Bearer YOUR_PRAL_TOKEN' \
  -H 'Content-Type: application/json' \
  -d @invoice.json

Enter fullscreen mode Exit fullscreen mode

Or in Python:

import requests
import os

PRAL_TOKEN = os.environ['PRAL_BEARER_TOKEN']
ENDPOINT = 'https://gw.fbr.gov.pk/di_data/v1/di/postinvoicedata'

response = requests.post(
    ENDPOINT,
    headers={
        'Authorization': f'Bearer {PRAL_TOKEN}',
        'Content-Type': 'application/json'
    },
    json=invoice_payload,
    timeout=30
)

if response.status_code == 200:
    result = response.json()
    irn = result['invoiceNumber']
    qr_code = result['QRCode']  # base64-encoded image
    # Persist IRN + QR code with your invoice record
else:
    # Handle error (see error handling section below)
    pass

Enter fullscreen mode Exit fullscreen mode

Step 4: Print the IRN and QR code

The IRN and QR code must appear on the invoice you give the customer. Without them, the invoice is legally invalid — even if you have a paper copy.

Your invoice template needs:

  • The IRN clearly labelled (e.g., "FBR Reference: 12345678901234")
  • The QR code rendered at minimum 2cm × 2cm with sufficient contrast
  • A note: "Verify this invoice at fbr.gov.pk/online-invoice-verification"

Most stack PDFs use libraries like ReportLab (Python) or PDFKit (Node) to embed the base64 QR code directly.

Step 5: Sandbox first, always

FBR provides a sandbox environment at https://gw.fbr.gov.pk/di_data/v1/di/postinvoicedata_sb for testing. You must pass all 28 mandatory test scenarios before going to production.

The 28 scenarios fall into seven categories:

  1. Standard sales (5 scenarios) — basic B2B/B2C at 18%
  2. Third Schedule goods (3 scenarios) — retail-price-based tax
  3. Reduced-rate and zero-rated supplies (4 scenarios) — 1%/5%/10%/15% rates plus zero-rated exports
  4. Exempt supplies (2 scenarios) — Sixth Schedule items
  5. Further tax and withholding (4 scenarios) — 3% further tax for unregistered buyers
  6. Returns, debit and credit notes (4 scenarios) — linkage to original IRN
  7. Special cases (6 scenarios) — multi-currency, FED on services, advance payments, exports

Most integration failures happen on scenarios 3 (reduced rates) and 6 (returns linkage). Build automated tests for these first.

Step 6: Error handling and retry logic

FBR's API isn't always up. You need queue-and-retry logic in production.

A reasonable pattern:

class FBRSubmissionService:
    MAX_RETRIES = 5
    RETRY_DELAYS = [1, 5, 30, 120, 600]  # seconds, exponential

    def submit_invoice(self, invoice_id):
        invoice = Invoice.objects.get(id=invoice_id)
        payload = self.build_payload(invoice)

        for attempt, delay in enumerate(self.RETRY_DELAYS):
            try:
                resp = requests.post(
                    self.endpoint,
                    headers={'Authorization': f'Bearer {self.token}'},
                    json=payload,
                    timeout=30
                )

                if resp.status_code == 200:
                    result = resp.json()
                    invoice.irn = result['invoiceNumber']
                    invoice.qr_code = result['QRCode']
                    invoice.status = 'submitted'
                    invoice.save()
                    return True

                if resp.status_code == 400:
                    # Validation error — log + don't retry
                    invoice.status = 'validation_error'
                    invoice.error_details = resp.json()
                    invoice.save()
                    return False

                if resp.status_code in (500, 502, 503, 504):
                    # Transient error — retry
                    time.sleep(delay)
                    continue

            except requests.Timeout:
                time.sleep(delay)
                continue

        # Exhausted retries — flag for manual review
        invoice.status = 'requires_manual_review'
        invoice.save()
        return False

Enter fullscreen mode Exit fullscreen mode

Critical patterns:

  • Idempotency — your invoiceRefNo should be unique per submission so FBR won't double-process
  • Persistent queue — if your app crashes, pending submissions should resume on restart (use a database-backed queue, not in-memory)
  • Audit log — every submission attempt, response, retry should be logged with timestamps for compliance review
  • Alerting — set up alerts when invoices spend more than 1 hour in 'pending' state

Step 7: The 72-hour correction window

Under the April 2026 Sales Tax General Order No. 01 of 2026, invoices can be cancelled, deleted, or edited within 72 hours of issuance without Commissioner approval. After 72 hours, you need prior approval — slow.

Your software needs:

  • A timestamp-driven "editable" flag on every invoice
  • A correction endpoint that resubmits with reference to the original IRN
  • A clear UI hint that says "X hours remaining to edit without approval"

Don't let users edit invoices after 72 hours via your normal flow — force them through the Commissioner-approval path.

Step 8: Production deployment checklist

Before flipping the production switch:

  • [ ] All 28 sandbox scenarios passed
  • [ ] Bearer token stored encrypted (AES-256 at rest)
  • [ ] Token not logged in plain text anywhere
  • [ ] Idempotent invoiceRefNo generation
  • [ ] Queue-and-retry with exponential backoff
  • [ ] Persistent queue (survives app restart)
  • [ ] Audit log of every API call
  • [ ] Alerting on stuck invoices (>1 hour pending)
  • [ ] Monitoring of API response time (95th percentile)
  • [ ] Daily reconciliation: invoice counts you sent vs counts in FBR's portal
  • [ ] Backup plan for FBR downtime (when systems will queue locally and replay)
  • [ ] Per-business sandbox/production toggle if you serve multiple clients
  • [ ] QR code rendering tested on actual printed invoices (contrast, scannability)

Most integrations break in production on the reconciliation step — your audit log says you sent 1,000 invoices today, but FBR's portal shows 998. The missing two were rejected silently for some edge case. Catch these daily.

Step 9: Should you build this or use a managed platform?

Honest math:

  • Building it yourself: ~80–200 hours upfront, plus 5–15 hours per month maintenance (FBR updates the API several times a year)
  • Using a managed platform: Rs 1,500–8,000/month per business, zero engineering time

For a single-business integration, building yourself is defensible — you control everything, no vendor lock-in. For multi-business scenarios (tax consultants managing 10–100 clients), managed platforms always win on time-to-value.

I run Tallied, which handles this for Pakistani tax consultants. We charge Rs 1,500 per client business per month (wholesale tier) with white-label custom domains and a multi-client dashboard. For developers evaluating: we use the exact same PRAL API documented above, but consolidate multi-business token management, queue-and-retry, and the 28 sandbox scenarios into one interface.

For more context on the regulatory side of FBR Digital Invoicing — who must integrate, deadlines, penalties under Section 33, and how to choose the right software — see the full guide and the software comparison on the Tallied blog.

Closing notes for developers

The FBR Digital Invoicing API is well-designed by Pakistani government API standards — versioned, JSON-based, OAuth-token authenticated, with a real sandbox. The 28 scenarios are tedious but mostly straightforward. The biggest production gotchas are silent rejection, FBR downtime, and managing tokens across multiple registered persons if you're building multi-tenant.

Build idempotent. Log everything. Reconcile daily. Encrypt the token. Run all 28 scenarios in CI.

And if you ever ship anything in production against this API: print the IRN large enough that the chai-walla at your customer's office can read it. The compliance officer scanning your invoice with a phone camera will thank you.