How to Retrieve API Tokens with Python
To interact with the Indigo API, you must include a valid authentication token with your requests. This guide explains how to obtain this token.
Token Expiration and Renewal
The standard API token expires 30 minutes after issuance.
You have a couple of options for managing token expiration:
- Refresh Token: Use the User Refresh Token endpoint to get a new token.
- Automation Token: Alternatively, you can utilize the automation token and specify your desired expiration period.
Prerequisites
Before running the example script, ensure you have completed the following steps:
-
Install Python Library: Install the
requests
library if you haven't already. You can typically do this using pip:pip install requests
-
Configure Credentials: In the Python script provided below, you will need to insert your Indigo account details into the
USERNAME
andPASSWORD
variables.USERNAME
: Your Indigo account email address.PASSWORD
: Your Indigo account password. (MD5 encryption is handled by the script, so enter your plain password).
Python Script Example
The following Python script demonstrates how to sign in and retrieve an API token.
import json
import requests
import hashlib
# Define API endpoints and headers
INDIGO_BASE = "api.indigobrowser.com"
INDIGO_LAUNCHER = "https://launcher.indigobrowser.com:45011/api/v2"
LOCAL_HOST = "http://127.0.0.1"
HEADERS = {
'Accept': 'application/json',
}
# TODO: Insert your account information in both variables below.
USERNAME = "" # Replace with your Indigo email
PASSWORD = "" # Replace with your Indigo password
def sign_in():
"""
Signs into the Indigo API and retrieves an authentication token.
"""
# Prepare the payload with email and MD5 hashed password
payload = {
'email': USERNAME,
'password': hashlib.md5(PASSWORD.encode()).hexdigest()
}
try:
# Send a POST request to the sign-in endpoint
r = requests.post(f'{INDIGO_BASE}/user/signin', json=payload, headers=HEADERS)
r.raise_for_status() # Raises an HTTPError for bad responses (4XX or 5XX)
# Parse the JSON response
response_data = r.json() # Changed from json.loads(r.text) for better practice with requests
# Extract the token from the response
token = response_data.get('data', {}).get('token')
if token:
print(f"Successfully retrieved token: {token}")
return token
else:
print("\nFailed to retrieve token: 'token' field not found in response data.")
print(f"Response: {response_data}\n")
return None
except requests.exceptions.HTTPError as http_err:
print(f'\nHTTP error occurred: {http_err}')
print(f'Response content: {r.text}\n')
return None
except requests.exceptions.RequestException as req_err:
print(f'\nRequest error occurred: {req_err}\n')
return None
except json.JSONDecodeError:
print(f'\nFailed to decode JSON response: {r.text}\n')
return None
# Call the sign_in function to execute it and get the token
if __name__ == "__main__":
api_token = sign_in()
if api_token:
# You can now use this token for subsequent API calls
print("Token can be used for further API requests.")
else:
print("Failed to obtain API token.")