blogtraffic
v1.0.5
Published
The official SDK for handling BlogTraffic.io webhooks and events.
Maintainers
Readme
BlogTraffic Node.js SDK
The official SDK for handling BlogTraffic.io webhooks and events in Node.js applications.
Installation
npm install blogtrafficWebhook Features
- Built-in Event Listeners: Easily handle
onPublish,onUpdate, andonDeleteevents. - Signature Verification: Automatically verifies webhooks using your HMAC-SHA256 secret.
- CORS Support: Handles preflight requests automatically.
- TypeScript Support: Fully typed for a better developer experience.
Usage: Webhook Handler
Next.js (App Router)
The SDK handles all the heavy lifting: CORS, Authorization tokens, Timestamp validation, and Signature verification.
import { createBlogTrafficHandler } from 'blogtraffic';
const handler = createBlogTrafficHandler({
secret: process.env.BLOGTRAFFIC_SECRET!,
apiToken: process.env.API_TOKEN!, // Optional: validates Bearer token automatically
onPublish: async (blog) => {
// Add your DB logic here
console.log('Blog added:', blog.title);
}
});
export async function POST(req: Request) {
const body = await req.json();
const headers = Object.fromEntries(req.headers);
const result = await handler({ body, headers, method: req.method });
return new Response(JSON.stringify({ message: result.message }), {
status: result.status,
headers: result.headers
});
}
// Handle CORS preflight
export async function OPTIONS(req: Request) {
const result = await handler({ method: 'OPTIONS' });
return new Response(null, { status: result.status, headers: result.headers });
}Usage: Blog Client (Data Fetching)
The BlogTrafficClient allows you to fetch your published content directly from the BlogTraffic API.
Authentication: Requires both API token and secret key for secure access.
import { BlogTrafficClient } from 'blogtraffic';
// Initialize with both API token and secret key
const client = new BlogTrafficClient(
'your-api-token-here',
'your-secret-key-here'
);
// Fetch all blogs with pagination
const { data, pagination } = await client.getAllBlogs(1, 10);
// Get a single blog by ID
const blog = await client.getBlogById('blog-id-here');
// Get a blog by slug
const blog = await client.getBlogBySlug('hello-world');
// Get blogs by keyword
const { data } = await client.getBlogsByKeyword('javascript', 1, 10);Methods
| Method | Description |
| --- | --- |
| getAllBlogs(page, limit) | Returns a paginated list of all blogs. |
| getBlogById(blogId) | Returns a single blog by its ID. |
| getBlogBySlug(slug) | Returns a single blog by its slug. |
| getBlogsByKeyword(keyword, page, limit) | Returns blogs filtered by keyword. |
Express.js
const { createBlogTrafficHandler } = require('blogtraffic');
const handler = createBlogTrafficHandler({
secret: 'your_secret_here',
onPublish: (blog) => {
console.log('Published:', blog.title);
}
});
app.post('/api/webhook', async (req, res) => {
const result = await handler(req, res);
res.status(result.status).send(result.message);
});Security
Ensure you keep your secret safe. This secret is used to verify that the webhook requests are legitimately sent from BlogTraffic.io.
License
MIT
