Automate API Portal and SDK Publishing in Your DevOps Cycle

While providing API documentation and SDKs with your API makes developer onboarding faster, it can be a challenge to keep up when your API is constantly evolving. You need to manually publish the documentation or SDKs on every API update, which can be a time consuming and repetitive task, prone to delays in releases.

To cut down on time to market, you can integrate auto-generation of documentation and SDKs directly into your CI/CD pipelines or DevOps cycles. This helps automate API portal and SDK publishing so they are in sync with your API releases without any human intervention.

The APIMatic API is a popular automation option that can be integrated into your CI/CD pipelines seamlessly. You can set up GitHub Actions that are triggered on any update made to the API definition in your GitHub repo to update the published portal, and publish the SDKs automatically for the new API update. This results in instantaneous synchronization of the developer experience components for your API while making developers more productive, allowing them to focus on solving problems rather than repeating their tasks.

In this blog, I will take you through setting up a CI/CD process with GitHub Actions using the MessageMedia API to demonstrate the APIMatic API.

Step 1: Import an API into APIMatic and Publish Portal

Import the API into the APIMatic Dashboard by uploading the API definition file and publishing the portal with necessary customizations

Step 2: Upload the API Definition to GitHub Repository

In a GitHub repository, upload the API definition file in a folder and name it spec. This contains the MessageMediaAPI.json file.

Step 3: Integrate APIMatic Account with GitHub

Next, you need to integrate APIMatic with GitHub to directly deploy the SDK to your GitHub repository.

automate API Portal and SDK publishing

Step 4: Create Secrets for the API

The imported API needs to be authorized. Both the authentication key and the API entity id (ID that uniquely identifies the API) should remain confidential. The authentication keys can be found in the settings of your APIMatic account. Create an authentication key by choosing a descriptive name for it and clicking the Add button.

The API entity id can be found in the API integration key group shown below:

To create a secret, go to your GitHub repo and choose the Settings tab. Click Secrets > Actions on the left, and then choose New repository secret on the right panel. Create secrets for API_ENTITY_ID and API_KEY.

Step 5: Create Workflow to Automate API Portal and SDKs Publishing

1.   On your GitHub repository, create an action workflow file by first clicking on the Actions tab and then on Set up a workflow yourself to create the .github/workflow YAML file.

automate API Portal and SDK publishing

2.    In the .github/workflows/ directory, create a new file called automation.yml and add the following code.

name: Update Portal
on:
  push:
    branches:
  	- master
	paths:
  	- 'spec/**' 	
  workflow_dispatch:
 


jobs:
  update-portal:
    runs-on: ubuntu-latest
 
    steps:
    -  name : checkout-repo
       uses: actions/checkout@v2
       with:
      	ref: 'master'
  	id: checkout-repo

This means that whenever you push the changes in the spec folder of the master branch, the workflow will trigger and workflow_dispatch allows you to trigger it manually as well.

    - name: install zip
  	run: sudo apt-get install zip unzip
  	
    - name: zip files
  	run: zip -r portal-input.zip *
  	working-directory: spec
 
    - name: list
  	run: zip -sf portal-input.zip
  	working-directory: spec

Some of the configuration points in the automation.yml file along with their description are listed below:

  • checkout-repo: To check out a repository so a workflow can access it.
  • install zip: To install the zip command in the Ubuntu runner.
  • zip files & list: To create the zip file named portal-input.zip of the spec folder.
  • Inplace-import: To import the modified API definition in place of the old definition in the APIMatic Dashboard. The following code shows an example:
- name: inplace-import
  	run: >-
    	curl -X PUT
    	--url 'https://www.apimatic.io/api/api-entities/${{ secrets.API_ENTITY_ID }}/import-via-file'
    	-H 'Authorization:X-Auth-Key ${{ secrets.API_KEY }}'
    	-H 'Accept:application/vnd.apimatic.apiEntity.full.v1+json'
    	-F 'file=@spec/portal-input.zip'
  • publish-portal: Used to publish the portal for the modified API specification. For this endpoint, the API must be hosted manually first through the APIMatic Dashboard.
   - name: publish-portal
  	run: >-
   	curl -X PUT
   	--url 'https://www.apimatic.io/api/api-entities/${{ secrets.API_ENTITY_ID }}/hosted-portal'
   	-H 'Content-Length:0'
   	-H 'Authorization:X-Auth-Key ${{ secrets.API_KEY }}'
  •  fetch api entity: Using the endpoint to fetch the API entity and saving the response to entity.json.
   - name: fetch api entity
  	run: >-
   	curl --location --request GET 'https://www.apimatic.io/api/api-entities/${{ secrets.API_ENTITY_ID }}'
   	--header 'Authorization:X-Auth-Key ${{ secrets.API_KEY }}'
   	--header 'Accept:application/json'
   	-o entity.json
  • Fetch generate sdk id: Use the endpoint to generate the SDK (.NET) and save the response to data.json.
   - name: Fetch generate sdk id
  	run: >-
   	curl -X POST
   	--url 'https://www.apimatic.io/api/api-entities/${{ secrets.API_ENTITY_ID }}/code-generations/generate'
   	-H 'Authorization:X-Auth-Key ${{ secrets.API_KEY }}'
   	-H 'Accept:application/json' -H 'Content-Type:application/x-www-form-urlencoded'
   	-d 'template=CS_NET_STANDARD_LIB'
   	-o data.json
  • Sdk data: To extract the id of the generated SDK from the data.json file in which the response of the above used generated SDK endpoint is saved.
  	# Optionally, use `jq` to pull one or more fields from the JSON to include in the publish sdk endpoint
    - name: Sdk data
  	id: sdk_data
  	run: echo '::set-output name=id::'$(jq -r '.id' data.json)

  • Entity data: To extract the encrypted Id of the API from entity.json file in which the response of the fetch API entity endpoint is saved.
    - name: Entity data  	<span class="hljs-built_in">id</span>: encrypt_id  	run: <span class="hljs-built_in">echo</span> <span class="hljs-string">'::set-output name=encryptedId::'</span>$(jq -r <span class="hljs-string">'.encryptedId'</span> entity.json)
  • api key data: To extract the apiKey of the API from entity.json file in which the response of the fetch API entity endpoint is saved.
 - name: api key data   
     id: key_data   
     run: echo '::set-output name=apiKey::'$(jq -r '.apiKey' entity.json)
  • publish-sdk: To publish the generated SDK in the GitHub repository. The repository should already be created in your GitHub account and inside the body, the parameter should be set to repoName and the accountName.
    - name: publish-sdk
  	run : >-
    	curl 'https://www.apimatic.io/api/GitDeploy/Deploy'
    	-H 'Authorization:X-Auth-Key ${{ secrets.API_KEY }}'
    	-H 'accept: application/json, text/plain, */*'
    	-H 'content-type: application/json;charset=UTF-8'
    	--data-raw '{"apiKey":"${{ steps.key_data.outputs.apiKey }}","encryptedId":"${{ steps.encrypt_id.outputs.encryptedId }}","template":"CS_NET_STANDARD_LIB","repoName":"your-GitHub-repo-name","branchName":"CodeGen-CS","downloadId":"/api/api-entities/${{ secrets.API_ENTITY_ID }}/code-generations/${{ steps.sdk_data.outputs.id }}/generated-sdk","commitMessage":"spec updated","accountName":"youGithubaccountname"}'
    	--compressed

Testing the GitHub Action

Once you have created the workflow, it is time to test how you can automate API portal and SDK publishing in your DevOps cycle. Commit the YAML file and make some changes in the API specification file (MessageMedia.json) that will trigger the workflow described.

You can see the status of the workflow as shown below:

Wrapping it up!

Through automation, developers spend more time improving their own API while the APIMatic API helps them get done through the boring and monotonous tasks. Likewise, GitHub Actions are definitely useful in streamlining your CI/CD pipelines and DevOps cycles and can be really helpful for those maintaining API definitions on GitHub. Check out more on how APIMatic supports automation for your developer experience!