Sling Academy
Home/Node.js/How fetch data from GitHub API with Node.js

How fetch data from GitHub API with Node.js

Last updated: December 30, 2023

Introduction

This guide dives deep into fetching data from the GitHub API using Node.js. If you’re interested in integrating GitHub’s wealth of data into your Node.js project, you’re in the right place. We’ll explore the GitHub API endpoints, and discuss authenticating requests, handling pagination, and processing webhook events.

Setting Up your Project

Begin by initiating a new Node.js project:

$ mkdir my-github-app
$ cd my-github-app
$ npm init -y

Install axios, a promise-based HTTP client:

$ npm install axios

Basic Fetching from the GitHub API

Let’s start with a simple GET request to retrieve a user’s public repositories:

import axios from 'axios';

const getUserRepos = async (username) => {
    try {
        const response = await axios.get(`https://api.github.com/users/${username}/repos`);
        console.log(response.data);
    } catch (error) {
        console.error(error);
    }
};

getUserRepos('octocat');

Authentication with the GitHub API

For more advanced requests, you might need to authenticate with the GitHub API. This is done by creating a personal access token at GitHub, and then including it in your request headers.

import axios from 'axios';

const getAuthenticatedUserRepos = async (token) => {
    const config = {
        headers: { Authorization: `token ${token}` }
    };
    try {
        const response = await axios.get('https://api.github.com/user/repos', config);
        console.log(response.data);
    } catch (error) {
        console.error(error);
    }
};

const token = 'YOUR_PERSONAL_ACCESS_TOKEN';
getAuthenticatedUserRepos(token);

Handling Pagination

When dealing with large sets of data, you might encounter pagination. Here’s how to handle it incrementally:

import axios from 'axios';

const fetchAllPages = async (username, token) => {
    let page = 1;
    let allRepos = [];
    while(true) {
        const config = {
            headers: { Authorization: `token ${token}` },
            params: { page }
        };
        const response = await axios.get(`https://api.github.com/users/${username}/repos`, config);
        if (response.data.length === 0) break;
        allRepos = [...allRepos, ..stoi(response.data)];
        page++;
    }
    return allRepos;
};

const username = 'octocat';
const token = 'YOUR_PERSONAL_ACCESS_TOKEN';
fetchAllPages(username, token).then(repos => console.Log(repos));

Interacting with Webhooks

To set up a webhook listener in your Node.js app:

import express from 'express';
import bodyParser from 'body-parser';

const app = express();
app.use(bodyParser.json());

app.post('/webhook-listener', (req, res) => {
    const event = req.body;
    // Handle the event accordingly
    console.log(`Received event: ${event.type}`);

    res.status(200).json({ received: true });
});

app.listen(3000, () => console.log('Server listening on port 3000...'));

You’ll need to point the GitHub webhook at your server’s address and listen for the event types you’re interested in.

Conclusion

You’ve just gone through the entire process of fetching data from the GitHub API using Node.js, from simple GET requests to handling advanced cases like authentication and webhooks. As you build more intricate integrations, keep in mind rate limits and security concerns to keep your app running smoothly and safely.

Next Article: How to programmatically post to Twitter with Node.js

Previous Article: How to Create a Reddit Bot with Node.js

Series: Node.js Intermediate Tutorials

Node.js

You May Also Like

  • NestJS: How to create cursor-based pagination (2 examples)
  • Cursor-Based Pagination in SequelizeJS: Practical Examples
  • MongooseJS: Cursor-Based Pagination Examples
  • Node.js: How to get location from IP address (3 approaches)
  • SequelizeJS: How to reset auto-increment ID after deleting records
  • SequelizeJS: Grouping Results by Multiple Columns
  • NestJS: Using Faker.js to populate database (for testing)
  • NodeJS: Search and download images by keyword from Unsplash API
  • NestJS: Generate N random users using Faker.js
  • Sequelize Upsert: How to insert or update a record in one query
  • NodeJS: Declaring types when using dotenv with TypeScript
  • Using ExpressJS and Multer with TypeScript
  • NodeJS: Link to static assets (JS, CSS) in Pug templates
  • NodeJS: How to use mixins in Pug templates
  • NodeJS: Displaying images and links in Pug templates
  • ExpressJS + Pug: How to use loops to render array data
  • ExpressJS: Using MORGAN to Log HTTP Requests
  • NodeJS: Using express-fileupload to simply upload files
  • ExpressJS: How to render JSON in Pug templates