Code Examples
Practical examples to get you started with the Content Hub API
List Pages
JavaScriptconst response = await fetch('https://contenhub.co/api/content', {
method: 'GET',
headers: {
'Authorization': 'Bearer your_api_key_here',
'Content-Type': 'application/json'
}
});
const data = await response.json();
console.log(data.items); // Array of pagesCreate Page with CTA
Pythonimport requests
content_data = {
'title': 'Get a Free Quote',
'content': 'Tell us what you need and we will respond fast.',
'slug': 'free-quote',
'meta_description': 'Request a free quote today',
'cta': {
'type': 'button',
'label': 'Request Quote',
'url': 'https://example.com/quote'
}
}
response = requests.post(
'https://contenhub.co/api/content',
json=content_data,
headers={'Authorization': 'Bearer your_api_key_here'}
)
result = response.json()
print(f"Created: {result['id']}")Error Handling
JavaScriptasync function createPage(pageData) {
try {
const response = await fetch('https://contenhub.co/api/content', {
method: 'POST',
headers: {
'Authorization': 'Bearer your_api_key_here',
'Content-Type': 'application/json'
},
body: JSON.stringify(pageData)
});
if (!response.ok) {
if (response.status === 400) {
throw new Error('Invalid page data');
}
if (response.status === 429) {
throw new Error('Rate limit exceeded');
}
throw new Error(`API Error: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('Page creation failed:', error.message);
throw error;
}
}Submit Analytics Event
PHP<?php
$apiKey = 'your_api_key_here';
$url = 'https://contenhub.co/api/analytics/view';
$data = json_encode([
'pageId' => 'page-id-123',
'timestamp' => date('c')
]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 200) {
echo "Analytics event recorded\n";
} else {
echo "Error: $httpCode\n";
}
?>Sync Pages from a CMS
Keep your Content Hub pages in sync with an external CMS or spreadsheet.
Node.js Sync Script
const API_KEY = process.env.CONTENT_HUB_API_KEY;
const BASE_URL = 'https://contenhub.co/api/content';
async function syncPages(pages) {
for (const page of pages) {
try {
const response = await fetch(BASE_URL, {
method: 'POST',
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(page)
});
if (!response.ok) {
const error = await response.json();
console.error('Sync failed:', page.slug, error);
continue;
}
const result = await response.json();
console.log('Synced:', result.id);
} catch (err) {
console.error('Network error:', page.slug, err.message);
}
}
}
const pages = [
{
title: 'Roofing Services Quote',
slug: 'roofing-services-quote',
content: 'Get a fast quote for roof repair and replacement.',
cta: { type: 'button', label: 'Get Quote', url: 'https://example.com/quote' }
}
];
syncPages(pages);Receive Lead Webhooks
Receive a notification every time a visitor submits a quote request or clicks a tracked CTA.
Express.js Handler
const express = require('express');
const router = express.Router();
router.post('/webhooks/content-hub', express.json(), (req, res) => {
const event = req.body;
switch (event.type) {
case 'lead.created':
console.log('New lead:', event.data.pageId, event.data.cta);
// Send to CRM, Slack, email, etc.
break;
case 'page.published':
console.log('Page published:', event.data.url);
break;
case 'page.updated':
console.log('Page updated:', event.data.id);
break;
}
res.json({ received: true });
});
module.exports = router;Common Event Types
lead.created
A visitor submits a quote request or CTA.
page.published
A page is published and assigned to network domains.
page.updated
An existing page is updated.
page.deleted
A page is removed from your account.
Build a Simple Analytics Dashboard
Fetch page data and render a small dashboard with the latest visits and leads.
React Dashboard Component
import { useEffect, useState } from 'react';
function PageDashboard() {
const [pages, setPages] = useState([]);
const [loading, setLoading] = useState(true);
useEffect(() => {
async function loadPages() {
const response = await fetch('/api/proxy/content', {
headers: { 'Authorization': 'Bearer your_api_key_here' }
});
const data = await response.json();
setPages(data.items || []);
setLoading(false);
}
loadPages();
}, []);
if (loading) return <p>Loading...</p>;
return (
<div>
{pages.map((page) => (
<div key={page.id} className="card">
<h3>{page.title}</h3>
<p>URL: {page.url}</p>
<p>Visits: {page.visits}</p>
</div>
))}
</div>
);
}Best Practices
- Always handle API errors and rate limits gracefully
- Keep API keys in environment variables, never in client-side code
- Do not send a domain field — Content Hub assigns pages automatically
- Use webhooks for lead notifications instead of polling
- Cache page lists when they do not change frequently