Blog API: Next.js Example
Blog API: Next.js Example
Full API reference: https://api.mailoo.app/docs/v1
This page shows how to integrate Mailoo's public Blog API into a Next.js app. The reference implementation is the blog in this repository at en/blog: list via BFF, article by slug via BFF, no API key on the client.
Prerequisites
- A Mailoo project with a Blog integration and at least one published article
- The project UID, integration ID, and an API key (dashboard → project → API Keys)
The exact values for MAILOO_BLOG_PROJECT_UID and MAILOO_BLOG_INTEGRATION_ID are shown on the integration settings page in the dashboard (Connection (External API) block). Use an API key with Blog (external read) scope.
Environment (server-only)
Add to .env.local:
MAILOO_BLOG_API=<same value as API_BASE_URL>
MAILOO_BLOG_API_KEY=your-api-key-here
MAILOO_BLOG_PROJECT_UID=<from dashboard integration page>
MAILOO_BLOG_INTEGRATION_ID=<from dashboard integration page>
Use server-side only; do not use NEXT_PUBLIC_* for API URL or key.
For the installable @mailoo/blog / @mailoo/next-core modules (GitLab Package Registry, route factories, components), see nextjs-packages{.interpreted-text role="doc"}.
List Page (e.g. app/blog/page.tsx)
Using BFF routes (same app exposes /api/v1/blog):
export default async function BlogPage() {
const res = await fetch('/api/v1/blog?limit=20', { next: { revalidate: 60 } })
if (!res.ok) return <div>Failed to load blog</div>
const json = await res.json()
const posts = json.data || []
return (
<div>
<h1>Blog</h1>
<ul>
{posts.map((p: { id: string; title: string; slug: string }) => (
<li key={p.id}><a href={`/blog/${p.slug}`}>{p.title}</a></li>
))}
</ul>
</div>
)
}
Optional: Categories (e.g. for filters)
To show category filters or links, fetch categories from the BFF:
const res = await fetch('/api/v1/blog/categories', { next: { revalidate: 60 } })
const json = await res.json()
const categories = json.data || [] // [{ id, slug, name }, ...]
Use categoryId in the list query when filtering: /api/v1/blog?categoryId=... (see blog-headless-cms{.interpreted-text role="doc"}).
Article Page (e.g. app/blog/[slug]/page.tsx)
Use BFF route /api/v1/blog/slug/[slug] or call the API with X-API-Key server-side. Return 404 if not found. For localized article content, pass the current locale in the query (e.g. ?locale=en); see blog-headless-cms{.interpreted-text role="doc"}.
SEO metadata: Prefer post.metaTitle ?? post.title, post.metaDescription ?? post.excerpt, post.ogImageUrl (else first body image), and post.canonicalUrl (else build from site origin + locale + slug) in generateMetadata (set alternates.canonical). Optional JSON-LD: GET /api/v1/blog/slug/{slug}/json-ld --- if placeholders remain, replace {{canonicalUrl}} / {{origin}} / {{locale}}; if the integration canonical template already filled absolute URLs, embed as-is. Optional keywords: join post.seoWords[].word and/or post.tags. Configure the template on the Blog integration Connection & settings tab (publicBaseUrl + path pattern such as /{locale}/blog/{slug}).
IndexNOW: To ping search engines when Mailoo publishes or updates articles, implement the integrator notify endpoint and set indexNow on the Blog integration --- see indexnow-notify{.interpreted-text role="doc"}.
import { notFound } from 'next/navigation'
export default async function BlogPostPage({ params }: { params: Promise<{ slug: string; locale: string }> }) {
const { slug, locale } = await params
const res = await fetch(`/api/v1/blog/slug/${encodeURIComponent(slug)}?locale=${encodeURIComponent(locale)}`, { next: { revalidate: 60 } })
if (!res.ok) notFound()
const json = await res.json()
const post = json.data
if (!post?.id) notFound()
const links = Array.isArray(post.linkedLinks) ? post.linkedLinks : []
return (
<article>
<h1>{post.title}</h1>
<p>{post.excerpt}</p>
<div dangerouslySetInnerHTML={{ __html: post.htmlContent || post.content }} />
{links.length > 0 && (
<section aria-labelledby="related-links">
<h2 id="related-links">Related links</h2>
{['internal_article', 'update_announcement', 'external_resource'].map((type) => {
const group = links.filter((l: { type: string }) => l.type === type)
if (!group.length) return null
return (
<div key={type}>
<h3 className="text-sm uppercase text-gray-500">{type}</h3>
<ul>
{group
.sort((a: { sortOrder: number }, b: { sortOrder: number }) => a.sortOrder - b.sortOrder)
.map((item: { id: string; label: string; url: string; intro?: string | null; date?: string | null }) => (
<li key={item.id}>
<a href={item.url.startsWith('/blog/') ? `/${locale}${item.url}` : item.url}>
{item.label}
</a>
{item.date && <p><time dateTime={item.date}>{new Date(item.date).toLocaleDateString()}</time></p>}
{item.intro && <p>{item.intro}</p>}
</li>
))}
</ul>
</div>
)
})}
</section>
)}
</article>
)
}
Printable summary blocks
When article Markdown includes a mailoo-print fenced block (see blog-headless-cms{.interpreted-text role="doc"}), htmlContent contains <section data-mailoo-print="true"> markers. Add a small client component after the article body to inject print buttons (same approach as BlogPrintableButtons in this repository):
'use client'
import { useEffect } from 'react'
function printMailooSection(section: HTMLElement) {
const body = section.querySelector('.mailoo-printable-body')
if (!body) return
const title = section.getAttribute('data-print-title')?.trim() || document.title
const win = window.open('', '_blank', 'noopener,noreferrer')
if (!win) return
win.document.write(`<!DOCTYPE html><html><head><meta charset="utf-8"><title>${title}</title></head><body>${body.innerHTML}</body></html>`)
win.document.close()
win.focus()
win.print()
}
export function BlogPrintableButtons() {
useEffect(() => {
document.querySelectorAll<HTMLElement>('[data-mailoo-print]:not([data-mailoo-print-enhanced])').forEach((section) => {
section.setAttribute('data-mailoo-print-enhanced', 'true')
const btn = document.createElement('button')
btn.type = 'button'
btn.textContent = 'Print summary'
btn.addEventListener('click', () => printMailooSection(section))
const actions = document.createElement('div')
actions.className = 'mailoo-printable-actions'
actions.appendChild(btn)
section.insertBefore(actions, section.firstChild)
})
}, [])
return null
}
Mount <BlogPrintableButtons /> next to the dangerouslySetInnerHTML article body wrapper.
Response shape and errors
For full response fields and error codes (e.g. 404 for unknown slug), see the API reference.