Quick Start Guide
Get up and running with The Prompting Company TypeScript SDK using the Next.js Agent MD pattern. This guide will walk you through creating a dynamic markdown serving system that allows users to access your agentic documentation through clean.md URLs.
What You’ll Build
By the end of this guide, you’ll have:- Clean URL routing: Users can access documents like
yoursite.com/docs/guide.md - Dynamic content serving: Documents are fetched from The Prompting Company platform
- Automatic middleware: URL rewriting handles
.mdextensions transparently - Scalable architecture: Support for nested document structures
How It Works
The implementation uses a two-part system:- Middleware: Intercepts requests ending in
.mdand rewrites them internally - API Route Handler: Fetches the actual markdown content using the SDK
Prerequisites
Before starting, make sure you have:- ✅ Installed the SDK
- ✅ A Next.js 13+ application with App Router
- ✅ Your API key, organization slug, and product slug
Step 1: Create the Middleware
First, create a middleware file to handle URL rewriting for.md extensions:
How the Middleware Works
- Intercepts Requests: Captures all requests except API routes and static files
- Detects .md Extensions: Checks if the URL ends with
.md - URL Rewriting: Transforms
/docs/guide.md→/md/docs/guideinternally - Transparent Process: Users see clean URLs, but internal routing handles the logic
Step 2: Create the MD Route Handler
Create the API route handler that will process the rewritten URLs and fetch markdown content:Understanding the Route Handler
- Dynamic Route:
[[...path]]captures multiple path segments (optional catch-all) - Next.js 15+ Pattern:
paramsis now a Promise that must be awaited - Parameter Destructuring:
{ path } = await paramsextracts the path segments - Path Processing: Joins segments like
['docs', 'api', 'auth']→"docs/api/auth" - SDK Integration: Uses the TypeScript SDK to fetch documents
- Error Handling: Returns proper HTTP status codes (404, 500)
- Content Type: Sets correct MIME type (
text/markdown; charset=utf-8)
Step 3: Understanding the Complete Flow
Here’s how the entire system works together:1. User Request
2. Middleware Processing
3. Route Handler Execution
4. Response
Step 4: Directory Structure
Your project should have this structure:Key Points:
- Middleware Location: Must be in
src/middleware.ts(or rootmiddleware.ts) - Dynamic Route:
[[...path]]captures any number of path segments - Route Handler: Only needs the
GETmethod for serving markdown - Error Handling: Proper HTTP status codes for missing documents
Step 5: Testing Your Implementation
1. Start Your Development Server
2. Test the URL Pattern
Your implementation should now handle these URL patterns: ✅ Working URLs:- Visit any URL ending in
.md - Check browser network tab - you should see:
- Request to
yoursite.com/docs/guide.md - Response with
Content-Type: text/markdown - Raw markdown content displayed
- Request to
3. Verify Document Paths
The URL structure maps to your organization’s document paths:4. Test Error Handling
404 Testing:- Visit
/nonexistent/document.md - Should return “Document not found” with 404 status
- Root level:
/readme.md→ document path:"readme" - Nested:
/api/auth/oauth.md→ document path:"api/auth/oauth"
Step 6: Command Line Testing (Optional)
For debugging, you can test the SDK directly:Benefits of This Pattern
The Next.js Agent MD pattern provides several advantages:✅ SEO-Friendly URLs
Clean URLs like/docs/guide.md are easily indexed by search engines
✅ Dynamic Content
Documents are fetched from your CMS in real-time, no static generation needed✅ Scalable Architecture
Supports unlimited nested document structures without additional routing✅ Proper MIME Types
Browsers receive correcttext/markdown content type for proper handling
✅ Error Handling
Built-in 404 handling for missing documents with proper HTTP status codesNext Steps
Congratulations! You now have a working Next.js Agent MD implementation. Here are ways to enhance it:Advanced Usage
Learn about caching, error handling, and optimization techniques.
GitHub Repository
View the complete reference implementation on GitHub.
Troubleshooting
Common Issues
Middleware not working:- Ensure
middleware.tsis in the correct location (src/middleware.tsor rootmiddleware.ts) - Check that your Next.js version supports middleware (12.2+)
- Verify the matcher configuration excludes necessary paths
- Confirm document paths in your organization match URL structure
- Check environment variables are set correctly (
TPC_API_KEY,TPC_ORG_SLUG,TPC_PRODUCT_SLUG) - Verify API key has
read:documentationpermissions
- Ensure the directory structure is correct:
src/app/md/[[...path]]/route.ts - Check that the route file exports a
GETfunction - Verify Next.js App Router is enabled (Next.js 13+)
- Test the SDK directly with the command line example
- Check network connectivity and API endpoint availability
- Verify organization and product slugs are correct