Fetching Blogs — Next.js, FastAPI & GraphQL
This guide shows three ways to fetch published blogs from BlogSaaS.
1. Test it in the browser first
After publishing a blog, visit:
http://localhost:3000/blog/your-blog-slug?workspace=your-workspace-slugThis is the built-in public blog viewer. No API key needed to preview.
To list all published blogs: http://localhost:3000/blog
2. REST API (any client)
First create an API key in Dashboard → API Keys, then:
# List published blogs
curl http://localhost:3000/api/v1/public/blogs \
-H "X-API-Key: bsk_your_key_here"
# Get single blog by slug
curl http://localhost:3000/api/v1/public/blogs/your-blog-slug \
-H "X-API-Key: bsk_your_key_here"3. Fetch from FastAPI (Python)
In your FastAPI app:
# requirements.txt
httpx
# main.py
import httpx
from fastapi import FastAPI
app = FastAPI()
BLOGSAAS_URL = "http://localhost:3000"
API_KEY = "bsk_your_key_here"
@app.get("/blogs")
async def get_blogs():
async with httpx.AsyncClient() as client:
res = await client.get(
f"{BLOGSAAS_URL}/api/v1/public/blogs",
headers={"X-API-Key": API_KEY}
)
return res.json()
@app.get("/blogs/{slug}")
async def get_blog(slug: str):
async with httpx.AsyncClient() as client:
res = await client.get(
f"{BLOGSAAS_URL}/api/v1/public/blogs/{slug}",
headers={"X-API-Key": API_KEY}
)
return res.json()Run it: uvicorn main:app --reload --port 8000
Then visit: http://localhost:8000/blogs
4. GraphQL API
BlogSaaS exposes a GraphQL endpoint at /api/graphql.
# Query published blogs
query GetBlogs($limit: Int) {
blogs(limit: $limit, status: PUBLISHED) {
id
title
slug
excerpt
readingTime
publishedAt
author {
name
avatarUrl
}
categories {
name
slug
}
tags {
name
slug
}
}
}
# Variables
{ "limit": 10 }Send the request:
curl -X POST http://localhost:3000/api/graphql \
-H "Content-Type: application/json" \
-H "X-API-Key: bsk_your_key_here" \
-d '{
"query": "query { blogs(status: PUBLISHED, limit: 5) { id title slug excerpt } }"
}'Python + GraphQL
import httpx
QUERY = """
query {
blogs(status: PUBLISHED, limit: 10) {
id title slug excerpt readingTime publishedAt
author { name }
}
}
"""
async def fetch_blogs():
async with httpx.AsyncClient() as client:
res = await client.post(
"http://localhost:3000/api/graphql",
json={"query": QUERY},
headers={"X-API-Key": "bsk_your_key_here"}
)
data = res.json()
return data["data"]["blogs"]Next.js + GraphQL
// lib/blogsaas.ts
const ENDPOINT = process.env.BLOGSAAS_URL + '/api/graphql'
const API_KEY = process.env.BLOGSAAS_API_KEY!
export async function fetchBlogs() {
const res = await fetch(ENDPOINT, {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'X-API-Key': API_KEY },
body: JSON.stringify({
query: `query {
blogs(status: PUBLISHED, limit: 10) {
id title slug excerpt readingTime
author { name }
}
}`
}),
next: { revalidate: 60 }
})
const { data } = await res.json()
return data.blogs
}5. Complete flow to test
- Go to Dashboard → Blogs → New Post
- Write a title and some content
- Click Publish
- Visit
/blog— your post appears - Create an API key in Dashboard → API Keys
- Run
curl http://localhost:3000/api/v1/public/blogs -H "X-API-Key: bsk_xxx" - Your blog JSON is returned ✓