Review Feature
You can use the service review feature to validate the results during the service call.
Note
This feature is only available for V2 API endpoints.
Example
- Node.js
- Python
- Curl
Nodejs
const axios = require("axios");
const api = "<API-V2>";
const headers = {
"x-aigen-key": "<KEY>",
"x-review-flow": true, //Enable review feature
"x-redirect-url": "https://example.com/callback", //Review result will be returned to your url
"x-review-expire": 3600, //Default 3600 sec
};
const data = { image: "<base64_string>" };
axios
.post(api, data, { headers: headers })
.then((res) => {
console.log(res.data);
})
.catch((err) => {
console.error(err.response.data);
});
Python
import requests
import json
api = "<API-V2>"
headers = {
"x-aigen-key": "<KEY>",
"x-review-flow": "true", # Enable review feature
"x-redirect-url": "<URL>", # Review result will be returned to your url
"x-review-expire": "3600" # Default 3600 sec
}
# Note: data should be a dictionary, requests will handle JSON encoding
data = {"image": "<base64_string>"}
res = requests.post(api, json=data, headers=headers) # Use json parameter for automatic encoding
print(res.json())
Curl
curl --location --request POST '<API-V2>' \
--header 'X-AIGEN-KEY: <KEY>' \
--header 'x-review-flow: true' \
--header 'x-redirect-url: <URL>' \
--header 'x-review-expire: 3600' \
--header 'Content-Type: application/json' \
--data-raw '{
"image" : "<base64_string>"
}'
After calling the service, you will be given a URL to check the results:
{
...,
"links": {
"review_url": "..."
}
}
After submitting, the review page will redirect to a page with parameters.
Parameter | Description |
---|---|
data | Corrected json response data in base64url encoded |
signature | Signature of data |
Verifying Signatures
To ensure that a request originates from AIGEN, it's important to verify its signature. This involves utilizing the HMAC-SHA256 algorithm with the API key.
- Using the HMAC-SHA256 algorithm with api key as the secret key.
- Compute the digest for the
data
. - Confirm that the base64 encoded digest matches the received
signature
.
import base64
import hashlib
import hmac
API_KEY = '...'
received_signature = b'...'
received_data = b'...'
decoded_data = base64.urlsafe_b64decode(received_data)
hash = hmac.new(API_KEY.encode('utf-8'), decoded_data, hashlib.sha256).digest()
signature = base64.urlsafe_b64encode(hash).rstrip(b'=')
if signature == received_signature:
print("signature verified")