Skip to content

Examples

Real-world examples for common FloImg API operations.

const response = await fetch('https://api.floimg.com/v1/workflows/execute', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.FLOIMG_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
workflowId: 'wf_abc123',
parameters: {
prompt: 'A serene mountain landscape at sunset',
width: 1200,
height: 630,
},
}),
});
const result = await response.json();
console.log(result.output.url);
// https://storage.floimg.com/images/xyz789.png
import { createReadStream } from 'fs';
import FormData from 'form-data';
const form = new FormData();
form.append('file', createReadStream('./image.png'));
form.append('projectId', 'proj_abc123');
const response = await fetch('https://api.floimg.com/v1/storage/upload', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.FLOIMG_API_KEY}`,
},
body: form,
});
const result = await response.json();
console.log(result.url);
// https://storage.floimg.com/images/abc123.png

For long-running workflows, use async execution:

// Start async execution
const startResponse = await fetch('https://api.floimg.com/v1/workflows/execute', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.FLOIMG_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
workflowId: 'wf_abc123',
parameters: { prompt: 'A detailed illustration' },
async: true,
}),
});
const { executionId } = await startResponse.json();
// Poll for completion
const pollExecution = async (id: string): Promise<any> => {
const response = await fetch(
`https://api.floimg.com/v1/workflows/executions/${id}`,
{
headers: {
'Authorization': `Bearer ${process.env.FLOIMG_API_KEY}`,
},
}
);
const result = await response.json();
if (result.status === 'pending' || result.status === 'running') {
await new Promise(r => setTimeout(r, 1000));
return pollExecution(id);
}
return result;
};
const result = await pollExecution(executionId);
console.log(result.output.url);
async function executeWorkflow(workflowId: string, parameters: object) {
const response = await fetch('https://api.floimg.com/v1/workflows/execute', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.FLOIMG_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ workflowId, parameters }),
});
if (!response.ok) {
const error = await response.json();
switch (error.error.code) {
case 'rate_limited':
const retryAfter = response.headers.get('Retry-After');
throw new Error(`Rate limited. Retry after ${retryAfter}s`);
case 'unauthorized':
throw new Error('Invalid API key');
case 'not_found':
throw new Error('Workflow not found');
default:
throw new Error(error.error.message);
}
}
return response.json();
}
async function batchExecute(
workflowId: string,
items: object[],
concurrency = 5
) {
const results = [];
const queue = [...items];
const worker = async () => {
while (queue.length > 0) {
const item = queue.shift();
if (!item) break;
try {
const result = await executeWithRetry(
() => executeWorkflow(workflowId, item)
);
results.push({ success: true, data: result });
} catch (error) {
results.push({ success: false, error });
}
}
};
// Run workers in parallel
await Promise.all(
Array(concurrency).fill(null).map(() => worker())
);
return results;
}
async function executeWithRetry(fn: () => Promise<any>, maxRetries = 3) {
let delay = 1000;
for (let i = 0; i < maxRetries; i++) {
try {
return await fn();
} catch (error: any) {
if (error.message.includes('Rate limited') && i < maxRetries - 1) {
await new Promise(r => setTimeout(r, delay));
delay *= 2;
continue;
}
throw error;
}
}
}
// Usage
const results = await batchExecute('wf_abc123', [
{ prompt: 'Image 1' },
{ prompt: 'Image 2' },
{ prompt: 'Image 3' },
]);