How to Add a Blog to Next.js Using Blogjs (Complete Guide) | blogjs
Web Development8 min read
How to Add a Blog to Next.js Using Blogjs (Complete Guide)
Learn how to add a fully functional blog to your Next.js website using BlogJS. This step-by-step guide covers installing the blogjs-space npm package, connecting your BlogJS project, fetching blog posts through the API, creating dynamic blog pages, and displaying content with your own UI components. Build an SEO-friendly blog without creating or maintaining a separate CMS.
If you are building a website using Next.js and want to add a blog, you might be thinking about creating your own CMS or integrating an existing blogging platform. Building a CMS from scratch takes time because you need to manage blog posts, categories, tags, images, publishing, and other features.
But what if you could manage all your blog content separately and simply fetch it inside your Next.js application? This is where BlogJS comes in. BlogJS is a headless blogging platform that helps developers integrate blog functionality without building and maintaining a separate CMS.
In this guide, we will see how to integrate BlogJS into a Next.js project, install the npm package, connect your project, fetch blog posts, and create dynamic blog pages using the Next.js App Router.
What We Are Going to Build
By the end of this tutorial, you will have a simple blog section inside your Next.js website. You will manage content through the BlogJS dashboard and display it on your website using your own UI components.
Create a BlogJS project.
Publish blog posts from the dashboard.
Install the BlogJS npm package.
Connect your Next.js application with BlogJS.
Fetch and display blog posts.
Create dynamic pages for individual blog posts.
Add SEO metadata to blog pages.
Prerequisites
Before starting, make sure you have a basic understanding of Next.js, React, and JavaScript or TypeScript. You should also have Node.js installed on your system.
Node.js installed.
Basic knowledge of Next.js.
A Next.js project (or create a new one).
A BlogJS account.
A BlogJS project with published blog posts.
Step 1: Create a BlogJS Project
First, visit BlogJS and create your account. After signing in, create a new project for your Next.js website.
Each project has its own content, categories, tags, and team members. This makes it easy to manage blogs for multiple websites from a single account.
Once your project is created, start adding some blog posts. Add a title, description, content, featured image, category, and relevant tags. Publish your posts when they are ready.
Step 2: Create or Open Your Next.js Project
If you already have a Next.js application, you can use your existing project. Otherwise, create a new application using the following command.
npx create-next-app@latest my-blog
cd my-blog
For this tutorial, we will use the Next.js App Router with TypeScript. You can follow a similar approach with a JavaScript project as well.
Step 3: Install the BlogJS Package
Now install the official BlogJS npm package called blogjs-space. This package provides a client that makes it easier to communicate with BlogJS and fetch your blog content.
npm install blogjs-space
If you are using pnpm, Yarn, or Bun, you can install it using your preferred package manager.
Step 4: Generate Your BlogJS API Key
To access your blog content, you need an API key from your BlogJS project. Open your project dashboard, go to the API key settings, and generate or copy your API key.
We will use this key to authenticate requests from our Next.js application. Keep it secure and do not expose it in client-side components or public environment variables.
Step 5: Configure Environment Variables
Create a .env.local file in the root directory of your Next.js project and add your BlogJS API key.
BLOGJS_API_KEY=your_blogjs_api_key
Replace the example value with your actual API key. Since we are not using the NEXT_PUBLIC prefix, this variable will remain available only on the server side.
Step 6: Initialize the BlogJS Client
To keep our code clean and reusable, create a separate file for initializing the BlogJS client. For example, create a lib/blogjs.ts file.
import { BlogJS } from 'blogjs-space';
export const blogjs = new BlogJS({
apiKey: process.env.BLOGJS_API_KEY!,
});
Now we can import this client wherever we need to fetch blog content on the server side. This also avoids creating a new client in every component.
Step 7: Fetch Blog Posts
Let's fetch all published blog posts from BlogJS. Create a reusable function that gets the blog data using the client.
Here, we are using the getBlogs() method to retrieve blog posts. The returned data can then be used to display blog cards, create listing pages, or build other sections of your website.
Step 8: Create a Blog Listing Page
Now let's create a page where visitors can see all published blog posts. Inside the App Router, create a app/blog/page.tsx file.
This is a simple example of a server component fetching data and rendering blog cards. You can customize the layout according to your website design.
Note: The exact fields returned by the SDK depend on its response schema. Adjust properties such as id, title, and description according to the actual BlogJS response.
Step 9: Create Dynamic Blog Pages
Showing all blogs on one page is not enough. Visitors should also be able to open individual articles. For this, we can use dynamic routes in Next.js.
The [slug] folder allows Next.js to create a separate page for each blog post. For example, a post with the slug getting-started-with-nextjs can be accessed at /blog/getting-started-with-nextjs.
Next, use the appropriate BlogJS SDK method or content API endpoint to fetch an individual post using its slug. The following is a conceptual example; replace the lookup function with the actual method supported by your SDK version.
type Props = {
params: Promise<{ slug: string }>;
};
export default async function BlogDetailsPage({ params }: Props) {
const { slug } = await params;
// Fetch the blog post using its slug.
// Connect this function to the relevant BlogJS API method.
const blog = await getBlogBySlug(slug);
if (!blog) {
return <h1>Blog not found</h1>;
}
return (
<article className="mx-auto max-w-3xl px-6 py-12">
<h1 className="text-4xl font-bold">
{blog.title}
</h1>
<p className="mt-4 text-gray-600">
{blog.description}
</p>
<div className="prose mt-8">
{/* Render your blog content here */}
</div>
</article>
);
}
Remember to implement getBlogBySlug() using the actual single-post fetching method provided by your BlogJS SDK or API. Also, render rich text or HTML content safely according to the format returned by BlogJS.
Step 10: Add SEO Metadata
SEO is important for any blog. If you want your articles to appear in Google search results, every blog page should have a meaningful title, description, and canonical URL.
Next.js allows you to generate metadata dynamically for each blog post using the generateMetadata function.
Replace the example domain with your actual website domain. If your BlogJS content includes featured images, you can also add them to the Open Graph metadata so article links look better when shared on social media.
Step 11: Improve Blog Performance
Once your blog is working, you should also think about performance. Fetching content on every request may not be necessary for articles that do not change frequently. Next.js provides caching and revalidation options that can help reduce unnecessary API requests.
Use server components for server-side data fetching.
Cache blog responses where appropriate.
Use time-based or on-demand revalidation when content changes.
Optimize featured images with the Next.js Image component.
Avoid exposing private API keys in client-side code.
Handle API errors and empty blog responses properly.
Common Mistakes to Avoid
Using a public environment variable for a private API key.
Fetching the same blog data multiple times unnecessarily.
Forgetting to add SEO metadata to individual blog pages.
Not handling missing or unpublished blog posts.
Rendering rich text content without checking its format.
Ignoring caching and API request limits.
Why Use BlogJS Instead of Building Your Own CMS?
Building your own CMS sounds simple at first, but when you start adding features such as a content editor, categories, tags, media management, team members, and publishing workflows, the development effort increases quickly.
BlogJS provides a separate place to manage your blog content and makes that content available through APIs and its npm client. You can focus on your Next.js application instead of spending time maintaining blogging infrastructure.
Manage multiple blog projects from one dashboard.
Invite team members and manage their access.
Create and organize blog posts.
Fetch content using the BlogJS client.
Use your own frontend design and components.
Get started without building a custom CMS backend.
Final Thoughts
Adding a blog to your Next.js website does not have to be a complicated task. With BlogJS, you can manage your content separately, fetch published articles through the API, and build your own blog experience using Next.js components.
The biggest advantage is that you do not need to spend time building and maintaining a separate CMS. You can focus on your website, design, and application features while BlogJS takes care of blog content management.
If you are planning to add blogging functionality to your Next.js project, give BlogJS a try and make your blog integration process easier.
Build with blogjs
Use the blogjs TypeScript SDK and REST APIs to ship a headless blog inside your existing product.