Calling an API
Overviewβ
In this tutorial, we will walk through the steps needed to make an API call in Shipyard. This guide will accomplish that using Shipyard's native HTTP Blueprint. If you'd rather use your own Python script, the steps are provided to do that as well.
We will be calling GitHub's API to create an issue in a repository.
By the end of this tutorial, you'll be able to:
- Call an API using the Request native Blueprint
- Call an API using a Python code Vessel
Stepsβ
Step 1: Generating API Keyβ
- Head here and follow GitHub's guide to create an API Key.
- Under Repository Permissions, change the access for Issues to have read and write access and Metadata to have read-only access.
Step 2: Calling GitHub's APIβ
- In Shipyard, click New Fleet in the top left corner. This will take you to the Fleet Builder.
- Native Blueprint
- Code
- Under HTTP, select Requests. This will create a Vessel for you in the Fleet Builder.
- Under Vessel Name, enter
Create GitHub Issue
. - Under Method, select POST.
- Under URL, enter
https://api.github.com/repos/YOUR_GITHUB_USERNAME/YOUR_REPOSITORY_NAME/issues
and insert your username and repository name in the marked spots. - Under Content Type, select application/x-www-form-urlencoded.
- Under Authorization Header, enter
token YOUR_API_CREDENTIAL
, replacing YOUR_API_CREDENTIAL with your actual API credential from GitHub. - Under Message, enter
{"title":"YOUR_REPOSITORY_NAME","body":"Test Issue"}
, replacing YOUR_REPOSITORY_NAME with your actual repository name.
You can connect a Vessel to the Request Vessel to receive the API response by sending response.txt with Slack or Email or storing it in a Cloud Storage Container.
- Click Save & Finish on the bottom right of your screen. This will take you to a page letting you know that your Fleet has been created successfully.
- Click Run Now. This will take you to the Fleet Log page for your Fleet run.
- Under Code Vessels, select Python. This will create a Vessel for you in the Fleet Builder.
- Under Vessel Name, enter
Create GitHub Issue
. - Under File to Run, enter
create_github_issue.py
. - Under Code, enter:
import requests
import os
token = os.environ.get('API_TOKEN')
headers = {
'Authorization': f'token {token}',
'Content-Type': 'application/x-www-form-urlencoded',
}
data = '{"title":"YOUR_REPOSITORY_NAME","body":"Test Issue"}'
response = requests.post('https://api.github.com/repos/YOUR_GITHUB_USERNAME/YOUR_REPOSITORY_NAME/issues', headers=headers, data=data)
print(response.text)
- In the code, you will need to replace the instance of
YOUR_REPOSITORY_NAME
with your specific repository name andYOUR_GITHUB_USERNAME
with your GitHub name. - Click Environment Variables.
- Click Add Enivronment Variable.
- In the name field, enter
API_TOKEN
The environment variable that we just created is accessed in the code using the line os.environ.get('API_TOKEN').
- In the hidden field, enter the API Token that you generated earlier from GitHub.
- Click Python Packages.
- Click Add Python Package
- In the name field, enter requests.
- Click Save & Finish on the bottom right of your screen. This will take you to a page letting you know that your Fleet has been created successfully.
- Click Run Now. This will take you to the Fleet Log page for your Fleet run.
Step 3: Verify that the Issue was Createdβ
- Native Blueprint
- Code
- Once the Fleet has finished running successfully, click the green rectangle from the gantt chart to view the Vessel log.
- The Vessel log confirms that the request was successfully sent to the API.
- Over in Github, navigate to the repository where you created the issue.
- Select Issues.
- You can see the Issue that we created using the API listed.
You've successfully used Shipyard to create an issue in GitHub using an API call.
- Once the Fleet has finished running successfully, click the green rectangle from the gantt chart to view the Vessel log.
- The Vessel log has the response from the API printed. You should be able to see the url of the Issue created.
- Over in Github, navigate to the repository where you created the issue.
- Select Issues.
- You can see the Issue that we created using the API listed.
You've successfully used Shipyard to create an issue in GitHub using an API call.