Common Code Patterns

cURL

$curl --location 'https://termina.ai/api/v1/user/me' \
>--header 'Accept: application/json' \
>--header 'Authorization: Bearer <jwt_token_value>'

Refreshing JWT Token (Python Example)

JWT tokens issued by the Termina API expire after one hour for security reasons. Depending on your use case, implementing an auto-refresh mechanism can ensure uninterrupted access to your data. Here’s a suggested implementation:

1import time
2import jwt
3import requests
4
5class RefreshingToken:
6 def __init__(self, api_key, refresh_buffer=60):
7 self.api_key = api_key
8 self._refresh_buffer = refresh_buffer
9 self._token = None
10 self._base_api = "https://app.termina.ai/api/v1"
11
12 def token(self):
13 if self.api_key is not None and (
14 self._token is None
15 or (
16 jwt.decode(self._token, options={"verify_signature": False})["exp"] - self._refresh_buffer < time.time()
17 )
18 ):
19 self._refresh()
20 return self._token
21
22 def _refresh(self):
23 response = requests.post(f"{self._base_api}/auth/get-token", json={"api_key": self.api_key})
24 self._token = response.json()["access_token"]
25
26 def bearer_headers(self) -> dict:
27 return {"Authorization": f"Bearer {self.token()}"}
28
29api_key = "..."
30refreshing_token = RefreshingToken(api_key=api_key)
31
32response = requests.get(
33 "https://app.termina.ai/api/v1/user/me",
34 headers=refreshing_token.bearer_headers(),
35)
36print(response.json())

Uploading Files with Signed URLs (Python Example)

Our default upload and download endpoints supports files up to 10GB. Anything larger will require the use of the signed-urls.

1import requests
2
3base_url: str = "https://app.termina.ai/api/v1"
4group_id: int = ...
5deal_id: int = ...
6token: str = "..."
7
8# Create test file
9file_content = "This is a test file"
10file_name = "test_file.txt"
11
12with open(file_name, "w") as f:
13 f.write(file_content)
14
15headers = {"Authorization": f"Bearer {token}"}
16
17# Create File Entry
18file_resp = requests.post(
19 f"{base_url}/group/{group_id}/deal/{deal_id}/file",
20 headers=headers,
21 json={
22 "name": file_name,
23 "type": "financials",
24 },
25)
26file = file_resp.json()
27
28# Retrieve upload url from response
29signed_url = file["signed_url"]["url"]
30
31# Upload File Contents
32with open(file_name, "rb") as f:
33 requests.put(signed_url, data=f)

Downloading Files with Signed URLs (Python Example)

1import requests
2
3download_url_resp = requests.get(
4 f"{base_url}/group/{group_id}/deal/{deal_id}/file/{file_id}/signed-url",
5 headers=headers,
6)
7
8download_url = download_url_resp.json()["url"]
9
10# Download File
11with open("downloaded_file.txt", "wb") as f:
12 f.write(requests.get(download_url).content)