Generate E-Invoice API Integration
Electronic invoicing (e-Invoicing) is the exchange of the invoice document between a supplier and a buyer in an integrated electronic format. Invoicing is a common B2B practice and has been part of Electronic Data Interchange transactions.
In this system, B2B invoices are authenticated electronically by GSTN for further use on the common GST portal. All invoice information will be transferred from this portal to both the GST portal and e-way bill portal in real-time.
In this post, we are going to explore steps included in the integration of e-invoice generation from our own web application.
You will be eligible for an e-invoice if your company’s revenue is more than 50 cr to enable e-invoice service on https://einvoice1.gst.gov this website.
To enable e-invoice service you need to select one provider from the provider list.
After registration of API service, the service provider will share credentials for accessing API including API_URL, Public Key(.pem file), Subscription Key(string), Authentication URL, Username, and Password.
Use the above credentials to call two APIs using the POST method. First one for authentication and the second for generation of e-invoice.
For authentication API requests you need to add a Subscription key in the header.
Inside the authentication API body, you need to send JSON object including action, username, ForceRefreshAccessToken as false, encrypted password, and encrypted APP_KEY (Random 32 character string) also store nonencrypted APP_KEY for decryption of SEK.
Above header and body parameters may vary as per service providers.
Encryption mechanisms provided in the documentation are in the .net and Java language. Please refer to the following code for encryption of strings like APP_KEY and password in NodeJS.
After calling Authentication API successfully you will receive two strings Authtoken and encoded SEK in response. You need to decrypt SEK using non-encrypted APP_KEY using the following code.
Create an e-invoice JSON object by using the rules provided in the documentation. Encrypt that JSON object by decrypted SEK by using the following encryption technique.
Create an e-invoice JSON object by using the rules provided in the documentation. Encrypt that JSON object by decrypted SEK by using the following encryption technique. After encrypting the object you are ready to call API for e-invoice generation. Add GSTIN, Username, Authtoken, and Subscription Key in the header. In the body, you need to add objects including the Data key. Data contains encrypted e-invoice-object. Below is the structure of the header and body of the e-invoice generation API.
Call API with POST method. If this API call is successful then it will return encrypted e-invoice data in response.
In response, you will get an IRN number, date, and status of the e-invoice. you will also receive QRcode data.
Finally, on your regular invoice pdf, you need to add the IRN number and QRcode which represents the IRN number and date.
We know that accessing high-security API gets confusing while implementing. I have tried to explain the steps involved in the e-invoice generation process in a simplified way with code samples. Hit the clap button if you find this post helpful also feel free to comment if you have any doubts.
Thanks !!
> Tushar Kale