BloggerJS is a Node.js wrapper for the Google Blogger API v3, allowing you to interact with blogs, posts, pages, comments, and users programmatically.
- Retrieve blog details by ID or URL
- Manage blog posts: create, update, delete, and list
- Manage pages: create, update, delete, and list
- Manage comments: list, approve, and delete
- Retrieve user information and blogs
- Supports custom requests via
sendRequest
Install via npm:
npm install @jobians/bloggerjs
This library comes with a working credentials.json
file to help you get started quickly. You can either:
- Use the provided
credentials.json
as-is for testing. - Replace it with your own credentials if you have an existing Google Cloud project.
To replace the credentials, simply download your credentials.json
file from your Google Cloud Console and overwrite the existing credentials.json
file in the root directory.
const BloggerJS = require('@jobians/bloggerjs');
const blogId = 'your-blog-id';
const blogger = new BloggerJS(blogId);
(async () => {
try {
// Get Blog Details
const blog = await blogger.getBlog();
console.log(blog);
// Get Blog by URL
const blogByUrl = await blogger.getBlogByUrl('http://username.blogspot.com');
console.log(blogByUrl);
// List Posts
const posts = await blogger.getPosts({ maxResults: 5 });
console.log(posts);
// Get a Specific Post by ID
const postId = 'your-post-id';
const post = await blogger.getPost(postId);
console.log(post);
// Add a New Post
const newPost = await blogger.addPost({
title: 'My New Post',
content: 'This is the content of my new post!',
});
console.log(newPost);
// Update a Post
const updatedPost = await blogger.updatePost(postId, {
title: 'Updated Post Title',
content: 'Updated content of the post',
});
console.log(updatedPost);
// Delete a Post
await blogger.deletePost(postId);
console.log(`Post ${postId} deleted`);
} catch (error) {
console.error('Error:', error.message);
}
})();
Retrieve blog details using the blog ID provided during instantiation.
const blog = await blogger.getBlog();
console.log(blog);
Retrieve a blog's details using its URL.
Parameters:
blogUrl
(string) – The URL of the blog.
const blogByUrl = await blogger.getBlogByUrl('http://username.blogspot.com');
console.log(blogByUrl);
Retrieve a list of posts for the blog.
Optional Parameters:
maxResults
(number) – The maximum number of posts to return.
const posts = await blogger.getPosts({ maxResults: 10 });
console.log(posts);
Retrieve details of a specific post by its ID.
Parameters:
postId
(string) – The ID of the post.
const post = await blogger.getPost('your-post-id');
console.log(post);
Create a new post on the blog.
Parameters:
params
(object) – An object containing the post properties, such astitle
,content
, etc.
const newPost = await blogger.addPost({
title: 'New Post Title',
content: 'The content of the new post.',
});
console.log(newPost);
Delete a post by its ID.
Parameters:
postId
(string) – The ID of the post to delete.
await blogger.deletePost('your-post-id');
console.log('Post deleted');
Update an existing post.
Parameters:
postId
(string) – The ID of the post to update.params
(object) – An object containing the new post properties (e.g.,title
,content
).
const updatedPost = await blogger.updatePost('your-post-id', {
title: 'Updated Title',
content: 'Updated post content',
});
console.log(updatedPost);
Partially update a post using patch semantics.
Parameters:
postId
(string) – The ID of the post to update.params
(object) – An object containing the fields to update.
const patchedPost = await blogger.patchPost('your-post-id', {
content: 'Partially updated content',
});
console.log(patchedPost);
Retrieve a list of comments for a specific post.
Parameters:
postId
(string) – The ID of the post.params
(object) – Optional parameters such asmaxResults
.
const comments = await blogger.getComments('your-post-id', { maxResults: 5 });
console.log(comments);
Retrieve details of a specific comment by its ID.
Parameters:
postId
(string) – The ID of the post.commentId
(string) – The ID of the comment.
const comment = await blogger.getComment('your-post-id', 'comment-id');
console.log(comment);
Delete a specific comment by its ID.
Parameters:
postId
(string) – The ID of the post.commentId
(string) – The ID of the comment.
await blogger.deleteComment('your-post-id', 'comment-id');
console.log('Comment deleted');
Approve a comment that is awaiting moderation.
Parameters:
postId
(string) – The ID of the post.commentId
(string) – The ID of the comment.
await blogger.approveComment('your-post-id', 'comment-id');
console.log('Comment approved');
Retrieve a list of pages for the blog.
Optional Parameters:
maxResults
(number) – The maximum number of pages to return.
const pages = await blogger.getPages({ maxResults: 10 });
console.log(pages);
Retrieve details of a specific page by its ID.
Parameters:
pageId
(string) – The ID of the page.
const page = await blogger.getPage('your-page-id');
console.log(page);
Create a new page on the blog.
Parameters:
params
(object) – An object containing the page properties, such astitle
,content
, etc.
const newPage = await blogger.addPage({
title: 'New Page Title',
content: 'The content of the new page.',
});
console.log(newPage);
Update an existing page.
Parameters:
pageId
(string) – The ID of the page to update.params
(object) – An object containing the new page properties (e.g.,title
,content
).
const updatedPage = await blogger.updatePage('your-page-id', {
title: 'Updated Title',
content: 'Updated page content',
});
console.log(updatedPage);
Delete a page by its ID.
Parameters:
pageId
(string) – The ID of the page to delete.
await blogger.deletePage('your-page-id');
console.log('Page deleted');
Retrieve a list of blogs for the specified user.
Parameters:
userId
(string) – The ID of the user. Use'self'
to refer to the authenticated user.
const blogs = await blogger.getUserBlogs('self');
console.log(blogs);
Retrieve the authenticated user's details.
const user = await blogger.getUser();
console.log(user);
Log out and remove saved authentication credentials. This will delete the token.json
file, effectively logging you out of the Blogger API.
await blogger.logout();
You can extend this class to make any custom requests that aren't directly covered by the provided methods. The sendRequest
method allows you to make API calls by specifying the HTTP method, the endpoint, and any parameters.
Make a custom request to any Blogger API endpoint.
Parameters:
method
(string) – The HTTP method (e.g.,'get'
,'post'
,'delete'
, etc.).endpoint
(string) – The Blogger API endpoint in the formatresource.action
.params
(object) – The parameters to send with the request.
Example:
// Custom request to list posts with a specific label
const customRequest = await blogger.sendRequest('get', 'posts.list', {
blogId: 'your-blog-id',
labels: 'Technology',
maxResults: 5,
});
console.log(customRequest);
The sendRequest
method gives you the flexibility to interact with any part of the Blogger API, even those not directly wrapped by this library. You can refer to the official Blogger API v3 documentation for more information on available endpoints.
If you find my work helpful, you can support me by donating: