An authorization token allows you to make API requests and authenticate the user without manually entering credentials.
To obtain a token, follow these steps:
Create a Result block.
Add variables in the block:
email and password.Create an Information block.
Connect an arrow from the Result block to the Information block.
Create an Expression block.
Connect an arrow from the Information block to the Expression block.
Enter the following BPL expression in the block properties:
$currdate = @today
$token = fact.load("TOKEN", "token")
$token = str.split($token, " ")
$prevdate = $token.get(0)
$token = $token.get(1)
Add a Condition block.
Connect an arrow from the Expression block to the Condition block.
Create an Information block.
Connect an arrow from the Condition block.
Enter the message text: Existing token already retrieved.
Connect an arrow from the Condition block to this block with the Default output type.
Create an Information block.
Connect an arrow with the OR Condition output type from the Condition block to the Information block.
Add three conditions:
Create an Expression block.
Enter the following BPL expression in the block properties:
$response = http.sendRequest("https://iam.twin24.ai/api/v1/auth/login",
"POST",
{"email": $email, "password": $password, "ttl": 86400},
{})
$tokenStatus = $response.statusCode
sys.sleep(5000000)
{tokenStatus} != 200.$body = $response.body
$token = $body.get("token")
$saveToken = str.join([$currdate, $token], " ")
fact.save("TOKEN", "token", $saveToken)
After completing all the steps and running the script, the token will be saved in the {token} variable. Use this variable for authentication when sending API requests.