You need API credentials to connect to the Enin API. They are your "username" and "password". To obtain credentials for the Enin API you need to send an email to sales@enin.ai. If your company already has received credentials, please reuse those as API credentials are per organization. If you have received an auth file and have problems opening it, use this guide.
There exists many schemes to authentication to APIs. The ones Enin use are Basic and Token based authentication. Basic Auth is easy to use as it is simply a username and password that you send along with your api request. It is considered to be less secure for production systems to use but can serve as an easy and fast way to familiarize yourself with the API. Token authentication is more secure and strongly recommended for production systems. Token authentication works by posting a username and password to a separate endpoint. This endpoint will return a token that you can store securely and use to access the Enin API.
The authentication file when you open it should look like the following:
API authentication for You AS
Basic Auth Client ID: YOUR_BASIC_AUTH_CLIENT_ID
Basic Auth Client Secret: YOUR_BASIC_AUTH_CLIENT_SECRET
Auth0 Token Client ID: YOUR_AUTH0_TOKEN_CLIENT_ID
Auth0 Token Client Secret: YOUR_AUTH0_TOKEN_CLIENT_SECRET
File Password: YOUR_FILE_PASSWORD
Here "Basic Auth Client ID" and "Basic Auth Client Secret" are username and password for basic authentication, while "Auth0 Auth Client ID" and "Auth0 Auth Client Secret" are used to create temporary access tokens which can be used for authentication. The final "File Password" is the decryption key of the encrypted zip-file you receive from us.
You can access our OpenAPI/Swagger documentation for our APIs by using
YOUR_BASIC_AUTH_CLIENT_ID
and YOUR_BASIC_AUTH_CLIENT_SECRET
:
When you have obtained the authentication file, you can start integrating with the Enin API. We recommend using Token Auth for production code as it is more secure, however basic auth can be used to familiarize yourself with the API.
Basic authentication will require a static whitelisted IP in the future and is discouraged.
Basic authentication is available for quick integration, but discouraged in favor of token authentication. In the below python code, we use the request library and pass the basic authentication as the "auth" param in the request.
# Python 3.9
import requests
from pprint import pprint
data = requests.get(
"https://api.enin.ai/analysis/v1/company/NO917540640",
headers={
"accept": "application/json",
},
auth=(
'YOUR_BASIC_AUTH_CLIENT_ID',
'YOUR_BASIC_AUTH_CLIENT_SECRET',
),
).json()
print("Example Data")
pprint(data)
Token authentication is recommended for production applications. It works by passing
your YOUR_AUTH0_TOKEN_CLIENT_ID
and YOUR_AUTH0_TOKEN_CLIENT_SECRET
to https://login.enin.ai
using a HTTP POST request. You will get a token as a response
which can be used to access the APIs.
Every token received has an expiration timestamp and is meant to be used until it is expired. Only after expiration should a new one be created. Below we provide an example of how this can be done in python.
# Python 3.9
import requests
import json
import time
def load_api_bearer_token(api_name: str):
client_id = 'YOUR_AUTH0_TOKEN_CLIENT_ID' # fill these from auth file
client_secret = 'YOUR_AUTH0_TOKEN_CLIENT_SECRET' # fill these from auth file
assert api_name in ("analysis", "datasets")
token_file_name = f".{api_name}.token.json"
# Check if token file is already created, if not we create a new one.
try:
with open(token_file_name) as file:
token_json = json.load(file)
except FileNotFoundError as ex:
token_json = None
now = time.time()
if not token_json or now > token_json["expires_timestamp"]:
# Missing or outdated token file. Creating a new one.
response = requests.post(
"https://login.enin.ai/oauth/token",
headers={
"content-type": "application/json",
},
json={
"grant_type": "client_credentials",
"client_id": client_id,
"client_secret": client_secret,
"audience": f"https://api.enin.ai/{api_name}",
},
)
if response.status_code == 401:
raise Exception(
"401 Response received from auth api, have you remembered to "
"replace YOUR_AUTH0_TOKEN_CLIENT_ID YOUR_AUTH0_TOKEN_CLIENT_SECRET "
"with your client_id and client secret from enin Auth file?"
)
token_json = response.json()
token_json["expires_timestamp"] = now + token_json["expires_in"]
with open(token_file_name, "wt") as file:
json.dump(token_json, file)
return f"{token_json['token_type']} {token_json['access_token']}"
Here we cache the token received in a JSON file file for future use. Alternative implementations might use Redis or similar tech.
With the access to a new or cached bearer token we can now use it with the API:
import requests
from pprint import pprint
data = requests.get(
"https://api.enin.ai/analysis/v1/company/NO917540640",
headers = {
"accept": "application/json",
"Authorization": load_api_bearer_token(api_name="analysis"),
# load_api_bearer_token is defined in the codeblock in the previous section
}
).json()
pprint(data)