Skip to content

SDK Examples

SDK Examples

CI/CD Integration

Automatically announce releases from your CI pipeline:

deploy.ts
import { Flow } from '@flowdev/sdk';
const flow = new Flow(process.env.FLOW_API_KEY!);
async function announceRelease(version: string, changelog: string) {
await flow.posts.create({
channel: process.env.RELEASES_CHANNEL!,
content: `🚀 v${version} is now live!\n\n${changelog}`,
scheduledFor: 'now',
});
}
// Call from your CI script
announceRelease('2.0.0', 'New features and bug fixes');

CMS Webhook Handler

Publish content when a new blog post is published:

api/cms-webhook.ts
import { Flow } from '@flowdev/sdk';
const flow = new Flow(process.env.FLOW_API_KEY!);
export async function handleCmsWebhook(payload: {
title: string;
url: string;
excerpt: string;
}) {
await flow.posts.create({
channel: process.env.BLOG_CHANNEL!,
content: `📝 New post: ${payload.title}\n\n${payload.excerpt}\n\n${payload.url}`,
scheduledFor: new Date(Date.now() + 300000).toISOString(), // 5 min delay
});
}

Scheduled Content Queue

Queue a week’s worth of content:

import { Flow } from '@flowdev/sdk';
const flow = new Flow(process.env.FLOW_API_KEY!);
const content = [
{ day: 0, time: '09:00', text: 'Monday motivation!' },
{ day: 1, time: '14:00', text: 'Tuesday tip: ...' },
{ day: 2, time: '10:00', text: 'Wednesday wisdom: ...' },
// ...
];
async function scheduleWeek(startDate: Date, channelId: string) {
for (const item of content) {
const scheduledFor = new Date(startDate);
scheduledFor.setDate(scheduledFor.getDate() + item.day);
const [hours, minutes] = item.time.split(':');
scheduledFor.setHours(parseInt(hours), parseInt(minutes), 0, 0);
await flow.posts.create({
channel: channelId,
content: item.text,
scheduledFor: scheduledFor.toISOString(),
});
}
}

Webhook Handler

Process Flow webhooks:

import { verifyWebhookSignature } from '@flowdev/sdk';
export async function handleWebhook(req: Request) {
const signature = req.headers.get('x-flow-signature')!;
const body = await req.text();
const isValid = verifyWebhookSignature(
body,
signature,
process.env.FLOW_WEBHOOK_SECRET!
);
if (!isValid) {
return new Response('Invalid signature', { status: 401 });
}
const event = JSON.parse(body);
switch (event.type) {
case 'post.published':
console.log('Post published:', event.data.id);
break;
case 'post.failed':
console.error('Post failed:', event.data.id, event.data.error);
// Alert your team
break;
}
return new Response('OK');
}

Multi-Platform Post

Post to multiple channels at once:

import { Flow } from '@flowdev/sdk';
const flow = new Flow(process.env.FLOW_API_KEY!);
async function postToAll(content: string, channels: string[]) {
const results = await Promise.allSettled(
channels.map(channel =>
flow.posts.create({
channel,
content,
scheduledFor: 'now',
})
)
);
return results.map((result, i) => ({
channel: channels[i],
success: result.status === 'fulfilled',
error: result.status === 'rejected' ? result.reason : null,
}));
}