Developer

Demo Site: API Routes

This guide documents the custom API routes implemented in the Demo Site, including preview, revalidation, AI search, and other endpoints.

API Routes Overview

The Demo Site includes 7 API routes:

  1. /api/preview - Preview mode
  2. /api/preview/exit - Exit preview
  3. /api/revalidate - Cache revalidation webhook
  4. /api/dynamic-redirect - Dynamic content redirects
  5. /api/ai/search - AI-powered search
  6. /api/ai/agent - AI agent endpoint
  7. /api/contact - Contact form submission

Preview Routes

/api/preview

Enables preview mode for draft content.

Implementation:

Usage:

  • Called from middleware when agilitypreviewkey is detected
  • Validates preview key with Agility CMS
  • Enables Next.js draft mode
  • Redirects to preview URL

/api/preview/exit

Exits preview mode.

Implementation:

Revalidation Route

/api/revalidate

Webhook endpoint for cache invalidation.

Implementation:

Webhook Configuration:

  • URL: https://your-site.com/api/revalidate
  • Events: Content Published, Page Published
  • Security: Validate with AGILITY_SECURITY_KEY

Dynamic Redirect Route

/api/dynamic-redirect

Handles dynamic content redirects (e.g., ?ContentID=123).

Implementation:

AI Search Routes

/api/ai/search

AI-powered search using Azure OpenAI and Algolia.

Implementation:

Features:

  • Azure OpenAI integration
  • Algolia search tool
  • Streaming responses
  • CMS-configured prompts

/api/ai/agent

AI agent endpoint for conversational search.

Implementation: Similar to /api/ai/search but with agent capabilities.

Contact Route

/api/contact

Handles contact form submissions.

Implementation:

API Route Patterns

Error Handling

Authentication

Response Formatting

Best Practices

  1. Validate Input: Always validate request data
  2. Error Handling: Handle errors gracefully
  3. Security: Validate webhook signatures
  4. Logging: Log important events
  5. Type Safety: Use TypeScript for request/response types

Next: Deployment - Deployment configuration