To use the API endpoints, you need to access and activate the 3decision API. For this, the required steps are:
GET /auth/api/login
).Instructions on how to perform all these steps are documented in the following sections. Instructions from the 3decision API interface, Curl commands, and Python scripts are reported.
API Secret Keys are managed from the user interface.
https://3decision<customer>.discngine.cloud
)The API Secret Key by default has a validity of 1 Day.
To generate a valid token, you must authenticate yourself using a valid API Secret Key (described in the section above “Generate an API Secret Key”).
The generated token by default has an expiration time of 1 hour.
Several ways of generating a valid token are described below.
https://3decision-<customer>-api.discngine.cloud/full/
(NB: this link is unique for each customer).GET /auth/api/login
endpointDng-Api-Key
field.The request must be sent to the ‘/auth/api/login’ route (i.e. https://3decision<customer>-api.discngine.cloud/full/api/login
). Specify a header called ‘Dng-Api-Key‘ with the generated API Secret Key as the value.
curl -X 'GET' \
'https://3decision-<customer>-api.discngine.cloud/auth/api/login' \
-H 'accept: application/json' \
-H 'X-API-VERSION: 1' \
-H 'Dng-Api-Key: XXXXXXXXXXXXXXXXXXXX
You can use a Python script to generate the token from the API secret key. You need to import ‘json’ and ‘requests’ libraries.
import json
import requests
Then we define a function get_token()
to generate it.
def get_token(
login_endpoint: str, headers: dict[str, str], refresh: bool = False
) -> str:
"""Generate a new token from the API secret, or refresh the current one.
Parameters
----------
refresh : bool, optional
if refresh is set to True, the token will be regenerated, by default False
Returns
----------
str
The user token.
"""
def _login(endpoint: str) -> str:
response = requests.get(
endpoint,
headers=headers,
)
return response.json()["access_token"]
if not hasattr(get_token, "token") or refresh:
# Generate a new token if we don't have one, or if we want to refresh it
setattr(get_token, "token", _login(login_endpoint))
# Else, use the token already generated
return getattr(get_token, "token")
You can call it in a main
def main():
"""Call 3decision API."""
api_secret_key: str = "XXXXXXXXXX"
api_url: str = "https://3decision-<customer>-api.discngine.cloud"
login_endpoint: str = f"{api_url}/auth/api/login"
headers: dict[str, str] = {
"Dng-Api-Key": api_secret_key,
"Content-Type": "application/json",
"X-API-VERSION": "1",
}
# Setup request headers, with authentication token
token: str = get_token(login_endpoint, headers)
if __name__ == "__main__":
main()
To call 3decision API endpoints, you must have the authorization provided by the token (generated in the section above “Generate a token”).
The token must be added as an OAuth 2.0 token (i.e. the header ‘Authorization’ set to ‘Bearer token_value’). If a request is incorrectly authenticated or the token has expired, a response with a 401 status code (Unauthorized) will be returned.
Generated tokens expire after one hour, regardless of the expiration date of the DNG API Key.
Several ways of calling the 3decision API are described below.
From the API (https://3decision-<customer>-api.discngine.cloud/full
), click on "Authorize" on the top right of the page
Paste your Token (only the token value, without the quotation marks)
Now you can call all endpoints of the 3decision API
Each endpoint of the 3decision API can be called using dedicated routes. An example is the /projects
endpoint (that returns all accessible projects for the user linked to this token). An example of ‘curl’ command is:
#!/usr/bin/env bash
TOKEN=XXXXXXXXXX
curl -X 'POST' \
'https://3decision-<customer>-api.discngine.cloud/projects' \
-H 'accept: application/json' \
-H 'X-API-VERSION: 1' \
-H "Authorization: Bearer $TOKEN" \
-H 'Content-Type: application/json' \
-d '{
"has_auto_superposition": true,
"description": "My project description",
"label": "My unique project label",
"projectTypeCode": "GEN"
}'
You can add a call to the 3decision API in the main, after the generation of the token.
def main():
"""Call 3decision API."""
api_secret_key: str = "XXXXXXXXXX"
api_url: str = "https://3decision-<customer>-api.discngine.cloud"
login_endpoint: str = f"{api_url}/auth/api/login"
headers: dict[str, str] = {
"Dng-Api-Key": api_secret_key,
"Content-Type": "application/json",
"X-API-VERSION": "1",
}
# Setup request headers, with authentication token
token: str = get_token(login_endpoint, headers)
# Endpoint you'd like to use
endpoint: str = f"{api_url}/projects"
# Payload to send
payload = {
"has_auto_superposition": True,
"description": "My project description",
"label": "My unique label 2",
"projectTypeCode": "GEN",
}
headers: dict[str, str] = {
"Content-Type": "application/json",
"accept": "application/json",
"Authorization": f"Bearer {token}",
"X-API-VERSION": "1",
}
# Make request
response: requests.Response = requests.post(
endpoint,
headers=headers,
data=json.dumps(payload),
)
if response.status_code < 400:
print("Everything went fine, response:")
else:
print("Creation failed, response:")
print(json.dumps(response.json(), indent=True))
Several API endpoints are asynchronous, they send the request as a job and return results later.
It avoids to wait for the response of a long time job while it runs.
An example of asynchronous endpoint is the POST /structure-registration
endpoint.
When it is called, it returns a job ID (identifier) which can be used to retrieve the results (see "Structure Registration" for more details).
For some endpoints, there is an optional property called async
- short for
asynchronous.
async
is set to True
, the response is a queue name and a job ID (where the job is running)async
is set to False
, the response is directly returned in the response bodyWhen a job is run asynchronously, you can retrieve the results by using the endpoint GET /queues/{queue}/jobs/{job_id}
:
{queue}
and {job_id}
by the queue name and the job ID returned when posting the job asynchronously./search/chemistry
We post a search to perform on the molecule “ N1C=CC=C1” with a similarity of 0.65 with the similarity search method.
We get this results :
The property "id" in the response will let you to fill the {job_id} value, and the "queue" property for {queue}.
To retrieve a result from an asynchronous job, we use this endpoint GET /queues/{queue}/jobs/{job_id}
For our example, we have:
After clicking on Execute, we directly get the results of the search under the "returnvalue" property in the response:
/search/chemistry
You have to set the property "async" to false if you want a synchronous job.
And then you directly get the response.