AirOps provides an easy and secure way to bring your AirOps Workflows to end user web experiences. To facilitate secure access to our Workflow directly from client-side code, we have implemented a Client SDK with an authorization mechanism.
This document will guide you through the process of installing and integrating our Client SDK with your server-side code, enabling your end-users to securely access Workflows endpoints.
Checkout our apps and chat playgrounds with code examples.
Authentication
To authenticate with our API using the SDK, you'll need three pieces of information:
Your workspace ID: Settings > Workspace ID
User ID (any unique identifier will work)
Your API key: Settings > API Key
First, use the API key to hash your user id on your back-end server. This will result in a hashed user id that is unique to your API key and user ID combination.
Here's a sample implementation of the identity verification process for your server:
require"openssl"defuser_id_hash api_key ="YOUR_API_KEY"# your workspace token (keep safe!) user_id ="YOUR_USER_ID"# Convert your api key to bytes key = api_key.encode("utf-8")# Hash the message using HMAC-SHA256 and the key hash =OpenSSL::HMAC.hexdigest('sha256', key, user_id)end
import hashlibdefuser_id_hash(): api_key ="YOUR_API_KEY"# your workspace token (keep safe!) user_id ="YOUR_USER_ID" key = api_key.encode('utf-8')# Hash the message using HMAC-SHA256 and the keyhash= hmac.new(key, user_id.encode('utf-8'), hashlib.sha256).hexdigest()returnhash
constcrypto=require('crypto');constuserIdHash= () => {constapiKey="YOUR_API_KEY"; // your workspace token (keep safe!)constuserId="YOUR_USER_ID";// Convert your api key to a bufferconstkey=Buffer.from(apiKey,'utf-8');// Hash the message using HMAC-SHA256 and the keyconsthash=crypto.createHmac('sha256', key).update(userId).digest('hex');return hash;}
Once you have successfully initialized the SDK, you can begin using the methods available to interact with our API. Note that the methods will return promises.
// Execute an workflowconstresponse=airops.apps.execute({ appId:1, version:4, payload: {"inputs": {"param":"XXXXYYYYZZZZ","song":"XXXXYYYYZZZZ" } }});// Wait for resultconstresult=awaitresponse.result();// Do something with result.output// Cancel executionawaitresponse.cancel();
Example Response
The response from the execute method will contain the execution id that can be used to retrieve results later along with two methods to wait for results or cancel the execution:
In order to stream the Workflow results you will need to enable stream and pass a callback function to the execute method. Optionally you can pass some extra callback function to get a notification when the Workflow is finished.
const response = await airopsInstance.apps.execute({
appId: 9,
version: 38,
payload: {
"inputs": {
"topic": "XXXXYYYYZZZZ"
}
},
stream: true,
streamCallback: (data: { content: string } ) => {
// Do something with the data
},
streamCompletedCallback: (data: { content: string }) => {
// Do something with the data
},
});
Pull Results Async
You can implement your own pulling logic using the getResults method.
constresponse=awaitairopsInstance.apps.chatStream({ appId:2, message,streamCallback: (data: { token:string; }) => {// do something with data.token },streamCompletedCallback: (data: { result:string }) => {// do something with data.result },...(sessionId && { sessionId }),// optionally pass sessionId to continue chat.});// Wait for resultconstresult=awaitresponse.result;// Do something with result.result// Cancel executionawaitresponse.cancel();// Use session id to continue chatresponse.sessionId;
Example Response
The response from the chatStream method will contain the sessionId and a result method to wait for the response. In order to continue with the chat pass the session Id along with the message.
exportinterfaceChatStreamResponse { sessionId:string; result:Promise<{ result:string }>; // result is a promise that resolves when the execution is completed.}
Error Handling
try {awaitairops.apps.execute({ appId:2, version:7, payload: {"inputs": {"topic":"XXXXYYYYZZZZ" } } });} catch (e) {// do something with error.message}