Setup a third party client

    In our example we will use the GitHub Node.JS client OctoKit REST.js (opens new window).

    This guide could also be used to setup an Axios client instance.

    First you will have to install the client package in your application by running one of the following commands:

    npm install @octokit/rest

    To init the client, we will use the . Hooks let you add new features in your Strapi application.

    Lets create our GitHub hook.

    Path — ./hooks/github/index.js

    When the hook is created, we have set it to enabled in order for Strapi to load it. You will need to create or edit the file ./config/hook.js.

    Path — ./config/hook.js

    1. module.exports = {
    2. enabled: true,
    3. },
    4. },

    Now you can start your application, you should see a log my hook is loaded in your terminal.

    First lets update the config file to add your GitHub tokenSetup a third party client - 图2 (opens new window). By following the you will also find the way to use GitHub applications.

    Path - .env

    Now we have to load the GitHub client.

    Path — ./hooks/github/index.js

    And here it is.

    You can now use strapi.services.github everywhere in your code to use the GitHub client.

    To simply test if it works, let’s update the bootstrap.js function to log your GitHub profile.

    1. module.exports = async () => {
    2. const data = await strapi.services.github.users.getAuthenticated();
    3. console.log(data);

    Restart your server and you should see your GitHub profile data.