This package is no longer maintained and is deprecated. Please use the new package @airops/airops-js
, which provides enhanced features and improvements.
-
Server configuration
To authenticate with our API using the SDK, you'll need three pieces of information: your workspace id, user
id, and 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. Workspace id and API key can be found in your
workspace settings. Below is an example nodejs configuration.
const crypto = require('crypto');
const userIdHash = () => {
const apiKey = "YOUR_API_KEY";
const userId = "YOUR_USER_ID";
const key = Buffer.from(apiKey, 'utf-8');
const hash = crypto.createHmac('sha256', key)
.update(userId)
.digest('hex');
return hash;
}
-
Install the client SDK
npm i @airops/js_sdk
-
Identify the user
const airopsInstance = AirOps.identify({
userId: 'YOUR_USER_ID',
workspaceId: 'YOUR_WORKSPACE_ID',
hashedUserId: 'YOUR_USER_ID_HASH'
})
-
Use it to execute an app
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.
const response = await airopsInstance.apps.execute({
appId: 1,
version: 1,
payload: {
"inputs": {
"name": "XXXXYYYYZZZZ"
}
}
});
const result = await response.result();
await response.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:
interface ExecuteResponse {
executionId: number;
result: () => Promise<AppExecution>;
cancel: () => Promise<void>;
}
interface AppExecution {
airops_app_id: number;
id: number;
status: string;
output: string;
stream_channel_id: string;
}
-
Execute an app with streaming
In order to stream the app results you will need to enable stream and pass a callback function to the execute method.
Optionally you can pass an extra callback function to get a notification when the app is finished.
const response = await airopsInstance.apps.execute({
appId: 1,
version: 1,
payload: {
inputs: {
name: "XXXXYYYYZZZZ",
},
},
stream: true,
streamCallback: (data: { content: string }) => {
},
streamCompletedCallback: (data: { content: string }) => {
},
});
-
Pull results async
You can implement your own pulling logic using the getResults method.
const result = await airopsInstance.apps.getResults({
appId: 1,
executionId: response.executionId,
});
-
Error handling
try {
await airopsInstance.apps.execute({
appId: 1,
version: 4,
payload: {
inputs: { name: "XXXXYYYYZZZZ" },
},
});
} catch (e) {
}
-
Chat assistant
const response = await airopsInstance.apps.chatStream({
appId: 2,
message,
streamCallback: (data: { token: string; }) => {
},
streamCompletedCallback: (data: { result: string }) => {
},
...(sessionId && { sessionId }),
});
const result = await response.result;
response.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 sessionId along with the message.
export interface ChatStreamResponse {
sessionId: string;
result: Promise<{ result: string }>;
}