Introducing Hashnode SDK for TypeScript/JavaScript

phuctm97

Minh-Phuc Tran

Posted on December 26, 2020

Introducing Hashnode SDK for TypeScript/JavaScript

In order to implement an auto-crosspost workflow for my website, I created a TypeScript/JavaScript SDK for working with Hashnode blogging platform. It's built around Hashnode API.

There're several reasons for it:

  • Hashnode public API is still in alpha and subject to breaking changes. Experience using is a bit messy (what to expect in an alpha API), I had to write several workaround-like functions. To make my website implementation more relevant and easier to maintain, I need a stable API as well as separating as much non-relevant codebase as possible.

  • The Hashnode Christmas hackathon.

  • It's gonna be interesting if Hashnode collaborate on this, use in their product, make it official, as their first step open-sourcing their product.

Key concepts

  • Opinionated, has its own interfaces focusing on common blogging features, e.g. CRUD articles, comments, etc (what you usually do on Hashnode).

  • Abstracts away Hashnode API, which is currently in alpha and subject to breaking changes. Whereas, this SDK's API is considerably stable, its API won't change without a major release. As soon as Hashnode API changes, it will be updated to keep its API remain unchanged and function correctly.

  • Not flexible by design, you can't query less or more fields, these use cases are covered by Hashnode GraphQL API.

How & why?

I am actively using this SDK in my website to implement an auto-crosspost workflow, which runs daily. Hence, as soon as something breaks, I'll recognize and try to fix as soon as possible. Check out this workflow run to see how it is used in practice.

Usage

Install

yarn add hashnode-sdk-js
Enter fullscreen mode Exit fullscreen mode

API

Configure API key

Go to Hashnode account settings, create an API key and set it as environment variable HASHNODE_API_KEY at where you run your application, the SDK will automatically use it.

Find a user by username

import hashnode from "hashnode-sdk-js";

hashnode.findUser("phuctm97").then((data) => console.log(data));
Enter fullscreen mode Exit fullscreen mode
{
  id: '5fa3f68b47631a19e811f076',
  username: 'phuctm97',
  name: 'Minh-Phuc Tran',
  tagline: 'Engineer 👨🏻‍💻. Building open-source tools and tutorials ➡️ twitter.com/phuctm97',
  publication: {
    id: '5fa565080163314ab6d7deab',
    name: "Minh-Phuc Tran's Blog",
    domain: 'blog.phuctm97.com'
  }
}
Enter fullscreen mode Exit fullscreen mode

Other examples

See more examples.

Types

export type Publication = {
  id: string;
  name: string;
  domain: string;
};

export type User = {
  id: string;
  username: string;
  name: string;
  tagline: string;
  publication: Publication;
};

export type Article = {
  id: string;
  title: string;
  slug: string;
  url: string;
  canonicalURL?: string;
  contentMarkdown: string;
};
Enter fullscreen mode Exit fullscreen mode

Features

  • Find user.

  • Get a user's publication.

  • Create an article in a publication.

  • Update an article in a publication.

(Contributions are welcome)

Links

Check out the SDK's repository and NPM package for more details.

💖 💪 🙅 🚩
phuctm97
Minh-Phuc Tran

Posted on December 26, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related