Real-world examples of integrating SuperPrompts into your Node.js applications.
Build a REST API with Express that uses SuperPrompts for AI interactions:
import express from 'express';
import { SuperPrompts } from 'superprompts';
import OpenAI from 'openai';
const app = express();
const promptClient = new SuperPrompts({ apiKey: process.env.SUPERPROMPTS_API_KEY });
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
app.use(express.json());
app.post('/api/chat', async (req, res) => {
try {
const { message, promptId } = req.body;
// Get structured prompt from SuperPrompts
const { prompt: systemPrompt } = await promptClient.getPrompt(promptId);
// Use with OpenAI
const completion = await openai.chat.completions.create({
model: 'gpt-5.6-terra',
messages: [
{ role: 'system', content: systemPrompt },
{ role: 'user', content: message }
]
});
res.json({ response: completion.choices[0].message.content });
} catch (error) {
console.error('Error:', error);
res.status(500).json({ error: 'Failed to process request' });
}
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});Use SuperPrompts in Next.js 14+ with the App Router:
// app/api/chat/route.ts
import { SuperPrompts } from 'superprompts';
import { NextRequest, NextResponse } from 'next/server';
import OpenAI from 'openai';
const promptClient = new SuperPrompts({ apiKey: process.env.SUPERPROMPTS_API_KEY! });
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
export async function POST(request: NextRequest) {
try {
const { message, promptId } = await request.json();
// Get structured prompt
const { prompt: systemPrompt } = await promptClient.getPrompt(promptId);
const completion = await openai.chat.completions.create({
model: 'gpt-5.6-terra',
messages: [
{ role: 'system', content: systemPrompt },
{ role: 'user', content: message }
]
});
return NextResponse.json({
response: completion.choices[0].message.content
});
} catch (error) {
return NextResponse.json(
{ error: 'Failed to process request' },
{ status: 500 }
);
}
}Use SuperPrompts with Anthropic's Claude:
import { SuperPrompts } from 'superprompts';
import Anthropic from '@anthropic-ai/sdk';
const promptClient = new SuperPrompts({ apiKey: process.env.SUPERPROMPTS_API_KEY });
const anthropic = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });
const PROMPT_ID = 'e0a6c9d2-72e8-447e-a207-7e7f3a19be49';
async function chat(userMessage: string) {
// Get structured prompt
const { prompt: systemPrompt } = await promptClient.getPrompt(PROMPT_ID);
const message = await anthropic.messages.create({
model: 'claude-sonnet-5-5',
max_tokens: 1024,
system: systemPrompt,
messages: [
{ role: 'user', content: userMessage }
]
});
return message.content[0].text;
}
// Usage
const response = await chat('Hello, how can you help me?');
console.log(response);Implement caching to reduce API calls:
import { SuperPrompts } from 'superprompts';
const promptClient = new SuperPrompts({ apiKey: process.env.SUPERPROMPTS_API_KEY });
const cache = new Map<string, { content: string; timestamp: number }>();
const CACHE_TTL = 5 * 60 * 1000; // 5 minutes
async function getCachedPrompt(promptId: string): Promise<string> {
const cached = cache.get(promptId);
const now = Date.now();
// Return cached if valid
if (cached && (now - cached.timestamp) < CACHE_TTL) {
return cached.content;
}
// Fetch fresh prompt
const { prompt: content } = await promptClient.getPrompt(promptId);
cache.set(promptId, { content, timestamp: now });
return content;
}
// Usage
const prompt = await getCachedPrompt('e0a6c9d2-72e8-447e-a207-7e7f3a19be49');
console.log(prompt);Implement robust error handling:
import { SuperPrompts } from 'superprompts';
const promptClient = new SuperPrompts({ apiKey: process.env.SUPERPROMPTS_API_KEY });
async function getPromptWithRetry(
promptId: string,
maxRetries: number = 3
): Promise<string> {
let lastError: Error;
for (let i = 0; i < maxRetries; i++) {
try {
const { prompt } = await promptClient.getPrompt(promptId);
return prompt;
} catch (error) {
lastError = error as Error;
console.error(`Attempt ${i + 1} failed:`, error);
// Wait before retrying (exponential backoff)
if (i < maxRetries - 1) {
await new Promise(resolve =>
setTimeout(resolve, Math.pow(2, i) * 1000)
);
}
}
}
throw new Error(
`Failed to fetch prompt after ${maxRetries} attempts: ${lastError!.message}`
);
}
// Usage
try {
const prompt = await getPromptWithRetry('e0a6c9d2-72e8-447e-a207-7e7f3a19be49');
console.log(prompt);
} catch (error) {
console.error('Failed to fetch prompt:', error);
}Fetch multiple prompts efficiently:
import { SuperPrompts } from 'superprompts';
const promptClient = new SuperPrompts({ apiKey: process.env.SUPERPROMPTS_API_KEY });
async function getMultiplePrompts(promptIds: string[]) {
// Fetch all prompts in parallel
const prompts = await Promise.all(
promptIds.map(async (id) => (await promptClient.getPrompt(id)).prompt)
);
// Return as object with IDs as keys
return promptIds.reduce((acc, id, index) => {
acc[id] = prompts[index];
return acc;
}, {} as Record<string, string>);
}
// Usage
const PROMPT_IDS = {
customerSupport: 'e0a6c9d2-72e8-447e-a207-7e7f3a19be49',
salesAssistant: '76d77da8-95ba-46a2-8d07-eae78e41d069',
technicalSupport: '548a6665-cb7f-40a4-8a78-bb82e8ca74b0'
};
const prompts = await getMultiplePrompts(Object.values(PROMPT_IDS));
console.log(prompts[PROMPT_IDS.customerSupport]);
console.log(prompts[PROMPT_IDS.salesAssistant]);Add type safety to your integration:
import { SuperPrompts } from 'superprompts';
// Name your prompts once; the API takes their UUIDs
const PROMPTS = {
customerSupport: 'e0a6c9d2-72e8-447e-a207-7e7f3a19be49',
salesAssistant: '76d77da8-95ba-46a2-8d07-eae78e41d069',
technicalSupport: '548a6665-cb7f-40a4-8a78-bb82e8ca74b0'
} as const;
type PromptName = keyof typeof PROMPTS;
class PromptService {
private client: SuperPrompts;
private cache: Map<PromptName, string>;
constructor(apiKey: string) {
this.client = new SuperPrompts({ apiKey });
this.cache = new Map();
}
async getPrompt(name: PromptName): Promise<string> {
// Check cache first
const cached = this.cache.get(name);
if (cached) return cached;
// Fetch and cache
const { prompt } = await this.client.getPrompt(PROMPTS[name]);
this.cache.set(name, prompt);
return prompt;
}
clearCache(): void {
this.cache.clear();
}
}
// Usage
const service = new PromptService(process.env.SUPERPROMPTS_API_KEY!);
const prompt = await service.getPrompt('customerSupport');
// TypeScript will error if you use an invalid prompt name