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
module.exports = {
enabled: true,
},
},
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 token (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.
module.exports = async () => {
const data = await strapi.services.github.users.getAuthenticated();
console.log(data);
Restart your server and you should see your GitHub profile data.