Next.js
This page details recommended ways to use the @zapier/ai-actions
library with Next.js.
The examples here are using the Next.js app router.
For storage, these examples are using Vercel KV but any other form of server-side storage can be used.
It’s assumed that your Next.js app also has authentication set up, using a library such as NextAuth.js.
Set up environment variables
After generating your OAuth client, you can set your client ID and redirect URL inside of your .env
file:
The NEXT_PUBLIC_
prefix ensures that they can be referenced client-side.
Handle authentication server-side
Authentication with AI Actions can be handled fully server-side.
Generating the authorization URL
Here is an example of a button that will initiate authentication with AI Actions, using the server-side code from above.
Getting and storing the access token
To get the access token after the user has authenticated, a page needs to exist at NEXT_PUBLIC_ZAPIER_AI_ACTIONS_OAUTH_REDIRECT_URI
that will handle the OAuth flow.
Using the access token to create an AiActions
client
Once the token is stored, it can be used to get an AiActions
client that can be used to make API calls.
It can also be passed to the AiActionsProvider
if you’re using the @zapier/ai-actions-react
library.
Make sure that the refresh_token
for the OAuth token doesn’t leave the
server