Public API Documentation
Access published content from swarupbaral.in
1. Base URL
The base URL for all API requests is the domain of the website:
[BASE_URL] = https://swarupbaral.in
Use http://localhost:4321 in a local development environment.
2. Endpoint: GET /api/public/blogs
Purpose
This endpoint retrieves a complete list of all approved and published blog posts. It is intended for generating blog listings, feeds, or any interface that requires structured access to public blog content.
Request Details
| Detail | Value |
|---|---|
| Method | GET |
| URL | [BASE_URL]/api/public/blogs |
| Authentication | None (Public Endpoint) |
Response Structure (JSON Array)
The response is a JSON array. Each object in the array represents a blog post with full metadata and sanitized HTML content.
| Field Name | Type | Description |
|---|---|---|
id | string | Unique database document ID. |
title | string | Title of the blog post. |
slug | string | URL-friendly identifier used in /blogs/[slug]. |
authorName | string | Name of the author. |
categories | string[] | Categories assigned to the post. |
tags | string[] | Tags describing the content. |
publishedAt | string | ISO 8601 formatted publication date. |
readingTime | number | Estimated reading time in minutes (200 WPM). |
wordCount | number | Total number of words in the article. |
content | string | Full sanitized HTML content of the blog post. Must be rendered using a raw HTML method. |
Example Usage (JavaScript — Fetch API)
The example below demonstrates how to retrieve and display a list of blog posts using the native Fetch API.
const API_URL = 'https://swarupbaral.in/api/public/blogs';
async function fetchBlogs() {
try {
const response = await fetch(API_URL);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const blogs = await response.json();
const blogList = document.getElementById('blog-list');
blogList.innerHTML = '';
blogs.forEach(blog => {
const listItem = document.createElement('li');
const fullUrl = `https://swarupbaral.in/blogs/${blog.slug}`;
listItem.innerHTML = `
<h4>${blog.title}</h4>
<p>By ${blog.authorName} | Read Time: ${blog.readingTime} min</p>
<a href="${fullUrl}" target="_blank">Read Full Post</a>
<hr>
`;
blogList.appendChild(listItem);
});
} catch (error) {
console.error("Failed to fetch blogs:", error);
document.getElementById('blog-list').innerHTML =
'<p style="color: red;">Error loading content.</p>';
}
}
fetchBlogs(); Advanced Usage: Rendering Full Content
Since the content field contains raw HTML, it must be rendered using a method that supports HTML injection.
// React example:
function BlogContent({ htmlContent }) {
return (
<article
className="blog-post-body"
dangerouslySetInnerHTML={{ __html: htmlContent }}
/>
);
} 3. Error Handling
In case of an internal server failure, the API returns a standardized error response.
| Status Code | Description |
|---|---|
500 | Internal Server Error. This indicates a server-side processing issue. |
Example Error Response:
{ "error": "Failed to fetch public blogs." } 4. Future Endpoints
Additional public API endpoints may be introduced in future versions, such as single post retrieval, public project listings, category/tag filtering, and more.