Skip to main content

Overview

Add your Invent AI assistant to any Next.js application (App Router or Pages Router). Perfect for React-based applications with server-side rendering and static generation.

Installation

App Router (Next.js 13+)

1

Create Assistant Component

Create a client component for the assistant:
2

Add to Root Layout

Import and use in your root layout:
3

Add Environment Variable

Add to your .env.local:

Pages Router (Next.js 12 and below)

1

Create Assistant Component

2

Add to _app.tsx

User Authentication

Security Requirement: When using any user-* attributes (user-id, user-name, user-avatar), you must also provide user-hash. Both user-id and user-hash must be provided together, or neither should be provided. The user-hash must be generated on your backend using HMAC-SHA256 with your assistant’s secret key. Never expose the secret key to the client.

Server-Side Hash Generation

Next.js is perfect for secure hash generation with Server Components or API Routes.

Using Server Components (App Router)

Hash Generation Helper

Using API Routes (Pages Router)

Then fetch this data in your component:

Integration with Auth Libraries

NextAuth.js

Clerk

TypeScript Support

Add type declarations for the custom element:

Tips for Next.js

Server Components

Use Server Components for secure hash generation

Environment Variables

Keep INVENT_SECRET_KEY server-side only

TypeScript

Add type definitions for better DX

SEO Friendly

No impact on SEO or page performance

Troubleshooting

Solutions:
  • Check that the script is loaded after the component mounts
  • Verify assistant ID is correct
  • Check browser console for errors
  • Try clearing Next.js cache: rm -rf .next
Solutions:
  • Add type declarations file (see TypeScript Support section)
  • Ensure file is included in tsconfig.json
  • Restart TypeScript server
Solutions:
  • Verify secret key is set in environment variables
  • Ensure hash is generated server-side, not client-side
  • Check that both user-id and user-hash are provided
  • Validate HMAC-SHA256 implementation

Best Practices

  • Always use Server Components or API Routes for hash generation
  • Store secret key in .env.local (never commit to git)
  • Use NEXT_PUBLIC_ prefix only for non-sensitive variables
  • Test authentication in both development and production
  • Consider using middleware for auth logic