Integrating your application with the Federal Board of Revenue (FBR) APIs has become a vital requirement for businesses in Pakistan. Whether you are running a retail POS system, ERP, or a custom billing solution, API integration ensures that your invoices are officially registered with FBR and your business remains compliant with tax regulations.

At first glance, integration might look complex, but by breaking it into simple steps — authentication, validation, posting, and logging  the process becomes manageable. Let’s go through it with practical examples in both Sandbox (testing) and Production (live) environments.

Sandbox vs Production

Before coding, it’s important to understand the two environments FBR provides:

  • Sandbox (Testing Environment)

    • Endpoints contain _sb (for example: validateinvoicedata_sb).

    • Safe for testing and development.

    • Does not affect real FBR records.

  • Production (Live Environment)

    • Endpoints without _sb.

    • Every invoice submitted here is legally binding.

    • Should only be used once your application is fully tested.

Always test thoroughly in Sandbox before moving to Production.

Step 1: Authentication (Getting the Token)

Both environments require a Bearer Token to authorize API requests.

using System.Net.Http.Headers;
using System.Text;

var client = new HttpClient();
client.BaseAddress = new Uri(“https://gw.fbr.gov.pk/”);

var request = new HttpRequestMessage(HttpMethod.Post,
“auth/realms/fbr-data/protocol/openid-connect/token”);

request.Content = new FormUrlEncodedContent(new Dictionary<string, string>
{
{ “grant_type”, “password” },
{ “username”, “YourFBRUsername” },
{ “password”, “YourFBRPassword” },
{ “client_id”, “YourClientID” },
{ “client_secret”, “YourClientSecret” }
});

var response = await client.SendAsync(request);
var result = await response.Content.ReadAsStringAsync();

Console.WriteLine($”Auth Response: {result}”);

The response will return an access_token, which is required in all further API calls.

Step 2: Validating Invoice Data

Validation helps ensure your invoice data is correct before posting.

  • Sandbox URL:
    https://gw.fbr.gov.pk/di_data/v1/di/validateinvoicedata_sb

  • Production URL:
    https://gw.fbr.gov.pk/di_data/v1/di/validateinvoicedata

// use without _sb in production

var validateRequest = new HttpRequestMessage(HttpMethod.Post,
“https://gw.fbr.gov.pk/di_data/v1/di/validateinvoicedata_sb”);

validateRequest.Headers.Authorization = new AuthenticationHeaderValue(“Bearer”, authToken);
validateRequest.Content = new StringContent(invoiceJsonPayload, Encoding.UTF8, “application/json”);

var validateResponse = await client.SendAsync(validateRequest);
var validateResult = await validateResponse.Content.ReadAsStringAsync();

Console.WriteLine($” Validation Response: {validateResult}”);

Step 3: Posting the Invoice

Once validated, the invoice can be submitted.

  • Sandbox URL:
    https://gw.fbr.gov.pk/di_data/v1/di/postinvoicedata_sb

  • Production URL:
    https://gw.fbr.gov.pk/di_data/v1/di/postinvoicedata

// use without _sb in production

var postRequest = new HttpRequestMessage(HttpMethod.Post,
“https://gw.fbr.gov.pk/di_data/v1/di/postinvoicedata_sb”);

postRequest.Headers.Authorization = new AuthenticationHeaderValue(“Bearer”, authToken);
postRequest.Content = new StringContent(invoiceJsonPayload, Encoding.UTF8, “application/json”);

var postResponse = await client.SendAsync(postRequest);
var postResult = await postResponse.Content.ReadAsStringAsync();

Console.WriteLine($”Post Response: {postResult}”);

If successful, FBR will return a unique invoice number (fbrInvoiceNo) which should be stored in your database for auditing.

 

Conclusion

Integrating FBR APIs into your application is straightforward when you follow these steps:

  1. Authenticate to get the token.

  2. Validate invoice data in Sandbox.

  3. Post invoices after validation.

  4. Handle errors and maintain logs for compliance.

Always begin in Sandbox to refine your process and avoid issues. Once everything is tested, move to Production to register invoices officially.

By following this approach, your business remains compliant with FBR regulations while building trust and transparency with customers.