Contact Us 1-800-596-4880

Github Example

Let’s work with the GitHub API.

In the notebook, APIs are driven by RAML files (more about RAML). RAML files define an API’s endpoints and expected parameters.

To create an API client in a notebook, we use the built-in API.createClient method:

// Read about the GitHub at https://anypoint.mulesoft.com/apiplatform/popular/#/portals/organizations/52560d3f-c37a-409d-9887-79e0a9a9ecff/apis/7782/versions/7918
API.createClient('github', 'https://anypoint.mulesoft.com/apiplatform/repository/v2/organizations/52560d3f-c37a-409d-9887-79e0a9a9ecff/public/apis/7782/versions/7918/files/api.raml');

You can use any web-accessible RAML file or insert an API from the Anypoint API Portal by mousing over the ellipses button (dot between cells) and selecting Insert API.

Now that we have an API client (named github), we need to authenticate it:

// Authenticate github
// We do not supply credentials; the notebook will prompt us for them.
// Pass scopes array to only ask for the permissions we'll need.
API.authenticate(github);

Do not enter your tokens directly into a notebook. Any one that can access your notebook can have access to your tokens.

Authenticating without supplying credentials pops up a modal screen where you can safely enter the credentials.

Now that we have an authenticated client, we can explore the API.

Because the RAML file details all of the available endpoints, our github client knows all the endpoints you can access. This means you can explore the API using the notebook’s typeahead.

Let’s take a look at the endpoints available on our github client:

// Type a "." after github to trigger the typeahead.
// You'll see a number of available endpoints.
// Choose one and you'll see what you can do with it...
github

Since we asked for permission to access your gists in the authentication step, let’s take a look at them:

// Access your gists.
gistsResponse = github.gists.get();

The get() request on gists returns a response with a body, headers, and status.

The gists are stored in the body as an array, so let’s assign them to a variable:

gists = gistsResponse.body;

Now we have an array of your gists (actually, it’s only your first 30, since GitHub paginates them…​).

So, when did you create your most recent gist?

if (gists.length === 0) {
'You don\'t have any gists!';
} else {
'You created your most recent gist on ' + gists[0].created_at;
}

This is just a small example of what you can do with the GitHub API and the notebook. You can create your own notebook to further explore the GitHub API.