Introduction
The AI Actions API client can be used to interact with any of the API endpoints that AI Actions supports.
Below are a few examples of common uses for the client.
Creating a client
See the authentication guide for more information on how to authenticate.
import { AiActions } from "@zapier/ai-actions";
const aiActionsClient = new AiActions({
auth: {
apiKey: "YOUR-API-KEY",
token: "OAUTH-TOKEN",
},
});
Checking a user’s authentication
This is useful if you want to check if a user’s AI Actions OAuth token is valid.
try {
await aiActionsClient.checkAuth();
} catch (error) {
console.error("User is not authenticated", error);
}
Error handling
The @zapier/ai-actions
package exports an AiActionsHttpError
type that can be used to get JSON from errors.
import { AiActionsHttpError } from "@zapier/ai-actions";
try {
await aiActionsClient
.executeAction
();
} catch (error) {
if (error instanceof AiActionsHttpError) {
const parsedError = await error.response.json();
console.error("Error executing action", parsedError);
}
}
Listing a user’s actions
Actions can be created or edited either on the AI Actions website or using the @zapier/ai-actions-react
library
const actionList = await aiActionsClient.getActionList();
actionList?.results.forEach((action) => {
console.log(action.id, action.description);
});
Executig an action
See the Run a stored action guide for more information on the available parameters.
const result = await aiActionsClient.executeAction(
{
ai_action_id: "YOUR_ACTION_ID",
},
{
instructions: "Create a row with the following data....",
params: {
spreadsheet: {
mode: "guess",
value: "My Spreadsheet",
},
},
preview_only: false,
}
);
console.log(result?.resolved_params, result?.full_results);
Search for Zapier apps and actions
const appSearch = await aiActionsClient.searchApps({ query: "Google Sheets" });
appSearch?.results.forEach(async ({ name, app }) => {
console.log(name);
const actionSearch = await aiActionsClient.searchActions({
app,
query: "add spreadsheet row",
});
});
Give feedback on an action run
If an execution doesn’t work properly, you can provide feedback on it to help us improve the AI.
const result = await aiActionsClient.executeAction({
});
if (result) {
await aiActionsClient.rateExecution({
execution_log_id: result.execution_log_id,
rating: -1,
feedback: "It didn't work for me!",
});
}