NodeJS
const axios = require('axios');
const token = 'YOUR_TOKEN_HERE'; // Replace with your token
const createUrl = 'https://api.autocontentapi.com/Content/Create';
const pollInterval = 5000; // Poll every 5 seconds
// The request data to create the content
const requestData = {
resources: [
{ content: 'https://autocontentapi.com', type: 'website' },
{ content: '74% of businesses expect AI to increase productivity', type: 'text' }
],
text: 'Explain why automating with AutoContent API is a game changer for any business',
outputType: 'audio'
};
// Create the content request
async function createContent() {
const response = await axios.post(createUrl, requestData, {
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
'accept': 'text/plain'
}
});
return response.data;
}
// Poll for status until complete or error
async function pollStatus(requestId) {
const statusUrl = `https://api.autocontentapi.com/content/status/${requestId}`;
while (true) {
const response = await axios.get(statusUrl, {
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
'accept': 'application/json'
}
});
const { status, error_message, audio_url, audio_title } = response.data;
if (error_message) {
console.error('Error from status check:', error_message);
return;
}
if (status === 100) {
console.log('Content creation complete!');
console.log('Audio URL:', audio_url);
console.log('Audio Title:', audio_title);
return;
}
console.log(`Current status: ${status}. Waiting for 100...`);
await new Promise(resolve => setTimeout(resolve, pollInterval));
}
}
// Main function
async function run() {
try {
const { request_id, error_message } = await createContent();
if (error_message || !request_id) {
console.error('Error from create request:', error_message);
return;
}
console.log(`Request initiated. Request ID: ${request_id}`);
await pollStatus(request_id);
} catch (err) {
console.error('Error:', err.response ? err.response.data : err.message);
}
}
run();
Last updated