Documentation of the Library
Session
The usual workflow in GST portal is as follows:
- Generate session token
- Use the token for accessing the APIs
The library works in the same manner:
from gst_irn import Session
session = Session(
# ... credentials
)
# generate token
session.generate_token()
# use the APIs
session.get_gst_info('GSTNUMBER')
The session object has following functions. All the functions handle the encryption and authentication automatically.
session.generate_token(force_regenerate_token=False, cache_dir="tokens_cache")
Generates token for the session. The auth session generated by the IRP (Invoice Registration Portal) is valid for 6 hours (1 hour in sandbox). The portal recommends re-using that auth token till expiry.
The library tries to cache the auth token and reuse it till expiry. The cache is saved in the cache_dir
. The directory is created if it doesn't exist.
Passing force_regenerate_token
as true force regenerates the token.
session.get(url, headers_extra=None)
Sends a GET
request to the given url. We can pass a dictionary of any extra headers if the API requires.
The function returns the decrypted JSON response.
session.post(url, data, headers_extra=None)
Sends a POST
request to the given url. The data / payload is automatically encrypted using the public key. We can pass a dictionary of any extra headers if the API requires.
The function returns the decrypted JSON response.
session.get_gst_info(party_gstin)
Returns the GST info for the given GST number (string). The structure of the JSON response is documented at https://einv-apisandbox.nic.in/version1.04/get-gstin-details.html#responsePayload.
session.generate_e_invoice(invoice)
Generates the IRN for the given invoice. The invoice
argument is a Python dict
. It is automatically converted to JSON internally.
The schema for the invoice
is available at https://einvoice1.gst.gov.in/Documents/EINVOICE_SCHEMA.xlsx
The JSON response is documented at https://einv-apisandbox.nic.in/version1.03/generate-irn.html#responsePayload.
Other API endpoints
The GST Portal has multiple other endpoints. These are for generating e-waybills, cancelling IRNs or fetching info of any doc.
These endpoints can be accessed using the session.get
and session.post
methods above.