AI Comments

This example shows how to build comments with Liveblocks and Next.js.

Image of AI Comments

Getting started

Run the following command to try this example locally:

$npx create-liveblocks-app@latest --example nextjs-comments-ai --api-key

This will download the example and ask permission to open your browser, enabling you to automatically get your API key from your liveblocks.io account.

Setting up webhooks

You need to set up webhooks to make this example run.

  • Follow our guide on testing webhooks locally, making sure to check the “commentCreated” event when creating the webhook
  • In the webhooks dashboard, point to the /api/ai-comment-reply path
  • Copy your webhook secret key from the webhooks dashboard
  • Add your webhook secret key to .env.local as the LIVEBLOCKS_WEBHOOK_SECRET_KEY environment variable

Setting up Anthropic

You need your own Anthropic API key to run the AI agent.

  • Create an account on Anthropic
  • Create a new API key from the Anthropic Dashboard
  • Add your Anthropic API key to .env.local as the ANTHROPIC_API_KEY environment variable

Manual setup

Alternatively, you can set up your project manually:

  • Install all dependencies with npm install
  • Create an account on liveblocks.io
  • Copy your secret key from the dashboard
  • Create an .env.local file and add your secret key as the LIVEBLOCKS_SECRET_KEY environment variable
  • Run npm run dev and go to http://localhost:3000
  • Follow the "Setting up webooks" and "Setting up OpenAI" sections above.

Deploy on Vercel

To both deploy on Vercel, and run the example locally, use the following command:

$npx create-liveblocks-app@latest --example nextjs-comments-ai --vercel

This will download the example and ask permission to open your browser, enabling you to deploy to Vercel.

Next, follow the "Setting up webooks" and "Setting up OpenAI" sections above.

Develop on CodeSandbox

After forking this example on CodeSandbox, create the LIVEBLOCKS_SECRET_KEY environment variable as a secret.

Next, follow the "Setting up webooks" and "Setting up OpenAI" sections above.