2019-12-18 15:40:29 +01:00
# Setup a third party client
2019-12-20 10:54:18 +01:00
This guide will explain how to setup a connection with a third party client and use it everywhere in your code.
2019-12-18 15:40:29 +01:00
In our example we will use the GitHub Node.JS client [OctoKit REST.js ](https://github.com/octokit/rest.js/ ).
This guide could also be used to setup an Axios client instance.
## Installation
2019-12-20 10:54:18 +01:00
First you will have to install the client package in your application by running one of the following command.
2019-12-18 15:40:29 +01:00
:::: tabs
::: tab yarn
`yarn add @octokit/rest`
:::
::: tab npm
`npm install @octokit/rest`
:::
::::
## Create a hook
2019-12-20 10:54:18 +01:00
To init the client, we will use the [hooks system ](../concepts/hooks.md ). Hooks let you add new features in your Strapi application.
2019-12-18 15:40:29 +01:00
Hooks are loaded one time, at the server start.
Lets create our GitHub hook.
**Path —** `./hooks/github/index.js`
```js
module.exports = strapi => {
2019-12-20 10:54:18 +01:00
return {
async initialize() {
2019-12-18 15:40:29 +01:00
console.log('my hook is loaded');
},
};
};
```
When the hook is created, we have to enable it to say to Strapi to use this hook.
**Path —** `./config/hook.json`
```json
{
...
"github": {
"enabled": true
}
}
```
Now you can start your application, you should see a log `my hook is loaded` in your terminal.
## Initialize the client
First lets update the config file to add your [GitHub token ](https://github.com/settings/tokens ).
By following the [documentation ](https://octokit.github.io/rest.js/#authentication ) you will also find the way to use GitHub applications.
**Path —** `./config/hook.json`
```json
{
...
"github": {
"enabled": true,
"token": "bf78d4fc3c1767019870476d6d7cc8961383d80f"
}
}
```
Now we have to load the GitHub client.
**Path —** `./hooks/github/index.js`
```js
const GitHubAPI = require('@octokit/rest ');
module.exports = strapi => {
2019-12-20 10:54:18 +01:00
return {
async initialize() {
2019-12-18 15:40:29 +01:00
const { token } = strapi.config.hook.github;
2019-12-20 10:54:18 +01:00
strapi.services.github = new GitHubAPI({
2019-12-18 15:40:29 +01:00
userAgent: `${strapi.config.info.name} v${strapi.config.info.version}` ,
auth: `token ${token}` ,
});
},
};
};
```
And here it is.
2020-02-08 23:31:41 -06:00
You can now use `strapi.services.github` everywhere in your code to use the GitHub client.
2019-12-18 15:40:29 +01:00
To simply test if it works, lets update the `bootstrap.js` function to log your GitHub profile.
**Path —** `./config/functions/bootstrap.js`
```js
module.exports = async () => {
2019-12-20 10:54:18 +01:00
const data = await strapi.services.github.users.getAuthenticated();
2019-12-18 15:40:29 +01:00
console.log(data);
};
```
Restart your server and you should see your GitHub profile data.
## Configs by environment
You would probably want specific configurations for development and production environment.
To do so, we will update some configurations.
2019-12-20 10:54:18 +01:00
You have to move your `github` configs from `./config/hook.json` to `./config/environments/development.json` , then remove it from the `hook.json` file.
2019-12-18 15:40:29 +01:00
And in your GitHub hook, you will have to replace `strapi.config.hook.github` by `strapi.config.currentEnvironment.github` to access to the configs.
**Path —** `./config/environments/development.json`
```json
{
"github": {
"enabled": true,
"token": "806506ab855a94e8608148315eeb39a44c29aee1"
}
}
```
2020-02-08 23:31:41 -06:00
**Path —** `./hooks/github/index.js`
2019-12-18 15:40:29 +01:00
```js
const GitHubAPI = require('@octokit/rest ');
module.exports = strapi => {
2019-12-20 10:54:18 +01:00
return {
async initialize() {
2019-12-18 15:40:29 +01:00
const { token } = strapi.config.currentEnvironment.github;
2019-12-20 10:54:18 +01:00
strapi.services.github = new GitHubAPI({
2019-12-18 15:40:29 +01:00
userAgent: `${strapi.config.info.name} v${strapi.config.info.version}` ,
auth: `token ${token}` ,
});
},
};
};
```