Documentation
Code Examples
Learn how to use the PromptAI API with examples in different programming languages.
Authentication
JAVASCRIPT
// Set up your API key
const API_KEY = 'your_api_key_here';
// Helper function for making authenticated requests
async function apiRequest(endpoint, options = {}) {
const url = `https://promptai.trymagic.xyz/api/v1${endpoint}`;
const headers = {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
...options.headers
};
const config = {
...options,
headers
};
const response = await fetch(url, config);
if (!response.ok) {
const error = await response.json();
throw new Error(error.message || 'API request failed');
}
return response.json();
}
Fetching Prompts
JAVASCRIPT
// Get all prompts
async function getAllPrompts(filters = {}) {
const queryParams = new URLSearchParams(filters).toString();
const endpoint = `/api/v1/prompts${queryParams ? `?${queryParams}` : ''}`;
return apiRequest(endpoint);
}
// Get a specific prompt
async function getPrompt(id, variables = {}) {
const queryParams = new URLSearchParams(variables).toString();
const endpoint = `/api/v1/prompts/${id}${queryParams ? `?${queryParams}` : ''}`;
return apiRequest(endpoint);
}
// Search prompts
async function searchPrompts(query, filters = {}) {
const params = { query, ...filters };
const queryParams = new URLSearchParams(params).toString();
const endpoint = `/api/v1/prompts/search?${queryParams}`;
return apiRequest(endpoint);
}
// Example usage
async function examples() {
// Get all AI prompts
const aiPrompts = await getAllPrompts({ category: 'AI', limit: 10 });
console.log(aiPrompts);
// Get a specific prompt
const prompt = await getPrompt(1);
console.log(prompt);
// Get a dynamic prompt with variables
const dynamicPrompt = await getPrompt(1, { topic: 'AI', tone: 'friendly' });
console.log(dynamicPrompt);
// Search prompts
const searchResults = await searchPrompts('chatbot', { category: 'AI' });
console.log(searchResults);
}
Error Handling
JAVASCRIPT
// Improved API request function with error handling
async function apiRequest(endpoint, options = {}) {
const url = `https://promptai.trymagic.xyz/api/v1${endpoint}`;
const headers = {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
...options.headers
};
const config = {
...options,
headers
};
try {
const response = await fetch(url, config);
// Check for rate limiting
if (response.status === 429) {
const retryAfter = response.headers.get('Retry-After') || 60;
throw new Error(`Rate limit exceeded. Try again in ${retryAfter} seconds.`);
}
// Handle other errors
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.message || `API error: ${response.status}`);
}
return response.json();
} catch (error) {
console.error('API request failed:', error);
throw error;
}
}
// Example usage with error handling
async function getPromptWithErrorHandling(id) {
try {
const result = await apiRequest(`/api/v1/prompts/${id}`);
return result;
} catch (error) {
if (error.message.includes('Rate limit exceeded')) {
console.log('Rate limit hit, will retry later');
// Implement retry logic here
} else if (error.message.includes('not found')) {
console.log('Prompt not found');
// Handle not found case
} else {
console.error('Unexpected error:', error);
}
return null;
}
}