Node.js Integration
Complete examples for integrating PrompTick agents in Node.js applications.
Installation
npm install axios
Basic Example
const axios = require('axios');
const PROMPTICK_API_KEY = process.env.PROMPTICK_API_KEY;
const AGENT_ID = 'agent_abc123';
const BASE_URL = 'https://api.promptick.ai/api/v1';
async function executeAgent(variables) {
try {
// Kick off execution (returns 202 Accepted immediately)
const response = await axios.post(
`${BASE_URL}/agents/${AGENT_ID}/execute`,
{ variables },
{
headers: {
Authorization: `Bearer ${PROMPTICK_API_KEY}`,
'Content-Type': 'application/json',
},
}
);
const { executionId } = response.data;
console.log('Execution started:', executionId);
// Poll for result
return await pollForResult(AGENT_ID, executionId);
} catch (error) {
console.error('Error:', error.response?.data || error.message);
throw error;
}
}
async function pollForResult(agentId, executionId, maxAttempts = 30) {
for (let i = 0; i < maxAttempts; i++) {
const response = await axios.get(
`${BASE_URL}/agents/${agentId}/executions/${executionId}`,
{
headers: {
Authorization: `Bearer ${PROMPTICK_API_KEY}`,
},
}
);
const { status, output, error } = response.data;
if (status === 'completed') {
return output;
}
if (status === 'failed') {
throw new Error(error);
}
// status === 'processing' — wait and retry
await new Promise(resolve => setTimeout(resolve, 2000));
}
throw new Error('Execution timeout');
}
// Usage
(async () => {
const output = await executeAgent({
product_name: 'Smart Watch Pro',
target_audience: 'fitness enthusiasts',
});
console.log('Output:', output);
})();
With Retry Logic
async function executeWithRetry(variables, maxRetries = 3) {
for (let i = 0; i < maxRetries; i++) {
try {
return await executeAgent(variables);
} catch (error) {
if (error.response?.status === 429) {
// Rate limit — exponential backoff
const delay = Math.pow(2, i) * 1000;
console.log(`Rate limited. Retrying in ${delay}ms...`);
await new Promise(resolve => setTimeout(resolve, delay));
continue;
}
throw error;
}
}
throw new Error('Max retries exceeded');
}
Express.js Integration
const express = require('express');
const app = express();
app.use(express.json());
app.post('/generate-description', async (req, res) => {
try {
const { productName, features } = req.body;
const output = await executeAgent({
product_name: productName,
features: features,
});
res.json({ description: output });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});