Fetching Articles Using the Hashnode API

I'm a full stack developer based in Australia. I write articles about Node.js, React, JavaScript & Web Development to make your learning journey easier ๐
Search for a command to run...

I'm a full stack developer based in Australia. I write articles about Node.js, React, JavaScript & Web Development to make your learning journey easier ๐
No comments yet. Be the first to comment.
Introduction Website performance is an important aspect of web development. It consists of mainly two things: The speed of your website How usable is your website for visitors? Importance of website performance Website performance is very importa...

There are plenty of native web APIs that have a variety of uses. Here are 5 useful APIs that I use quite often in my web development projects. Clipboard API The clipboard API is a simple method to copy text to a user's clipboard. This can be used in ...

Hey All ๐ In this article, we'll be covering the Fetch API in JavaScript how you can use it to make HTTP requests. What is the Fetch API? The fetch() method provides a simple way to make HTTP requests in JavaScript. It is natively in the browser, me...

Hey All ๐ In this article, we'll be covering 4 mistakes commonly made by people while learning to code that will seriously impact their coding journey. #1 - Copying and Pasting The first mistake I'll talk about is copying and pasting. We've all don...

Introduction There are plenty of units in CSS. From px to vh, there is an abundance of units all with their distinctive use cases. Considering the amount of options, its perfectly reasonably to be confused as to which unit you should use and where. T...

In this article, we'll be discussing how to fetch articles from your Hashnode blog using the Hashnode API.
The Hashnode API can be found at api.hashnode.com/. It is a GraphQL API that allows us to interact with Hashnode in a variety of ways. For instance, you can:
In this article, we'll just be covering how to fetch articles with the API.
Lets begin!
To fetch the articles, we will be making a POST request to the API from a JavaScript file using Fetch. Making the request:
const data = await fetch("https://api.hashnode.com/", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
query,
variables,
}),
});
This should be done inside an
asyncfunction forawaitto work
This won't work yet as we haven't defined the query or variables variables.
The query variable contains the actual GraphQL query we will use. This looks like this:
const query = `
query GetUserArticles($page: Int!) {
user(username: "BlakeYeboah") {
publication {
posts(page: $page) {
title
brief
slug
coverImage
}
}
}
}
`;
Replacing "BlakeYeboah" with your Hashnode username so it requests your articles
The variables variable is an object with values for variables mentioned in the query. In this case, its just the page variable which has to be an integer. We'll make this 0 to get the newest articles:
const variables = { page: 0 };
With these variables defined, our API request will now work. However, we must convert the JSON returned from the API into a usable JavaScript object for us to be able to access the posts we have fetched.
We can do this with the .json() method on the data variable which stores our request. We use await since it returns a promise. The code to do this looks like this:
const result = await data.json();
The actual articles are nested a few properties deep:
const articles = result.data.user.publication.posts;
If you want more information for each post, you can adjust the GraphQL query to select other properties as well. For a list of properties available, you can view the docs of the API.
In this article, you have learned how to fetch articles from the Hashnode API. This can be used in a variety of was and is definitely useful to know if you have a Hashnode blog. Recently, I used the API to display my latest blog posts on my personal portfolio website.
๐ Thanks for reading this article!
If you like what I do and would love to see more related content, follow me on my other social platforms:
GitHub: Blake-K-Yeboah
LinkedIn: Blake Yeboah
You can also show your support by buying me a coffee ๐