TypeScript SDK
Official, lightweight, and type-safe TypeScript SDK for the DITBlogs API.
View on NPM →Type-Safe
Lightweight
Promise-Based
Isomorphic
Installation
bash
npm install @dishistech/blogs-sdk
Quick Start
Instantiate the client with your API key and start making requests.
typescript
import { DITBlogsClient } from '@dishistech/blogs-sdk';
// It's recommended to store your API key in environment variables
const client = new DITBlogsClient(process.env.DITBLOGS_API_KEY!);
async function fetchRecentPosts() {
try {
console.log('Fetching the 5 most recent posts...');
const response = await client.getPosts({ limit: 5 });
console.log(`Found ${response.pagination.total} total posts.`);
response.posts.forEach(post => {
console.log(`- ${post.title} (slug: ${post.slug})`);
});
} catch (error) {
console.error('Failed to fetch posts:', error.message);
}
}
fetchRecentPosts();
API Reference
All methods return a Promise that resolves with data from the API.
new DITBlogsClient(apiKey)
Creates a new API client instance.
apiKey
RequiredYour secret API key from the DITBlogs dashboard.
Posts
client.getPosts(params)
Retrieves a paginated list of posts.
client.getPost(slug)
Retrieves a single post by its slug.
Categories
client.getCategories()
Retrieves a list of all categories.
client.getCategory(slug, params)
Retrieves a single category and its posts.
Tags
client.getTags()
Retrieves a list of all tags.
client.getTag(slug, params)
Retrieves a single tag and its posts.
Comments
client.getComments(postSlug)
Retrieves all comments for a post.
client.postComment(params)
Submits a new comment or a reply.
Error Handling
Always Handle Errors
If the API returns an error (any non-2xx status code), the promise will be rejected. You should wrap all API calls in a try...catch
block to handle failures gracefully.
typescript
async function fetchInvalidPost() {
try {
const post = await client.getPost('this-slug-does-not-exist');
console.log(post);
} catch (error) {
// error.message will contain the JSON error response from the API
console.error(error.message);
// Example output: API Error (404): "{\"error\":\"Post not found.\"}"
}
}