Using the DeployHQ API in scripts
Once you've gotten acquainted with our DeployHQ API, you may feel like you're missing some more in-depth functions, such as, for example, GET commands to retrieve specific information for each of your DeployHQ projects. Well, this is absolutely possible with scripting!
For this example, we will be running a Python 3
script to print out the zone for each one of your projects in your DeployHQ account.
First, we need to get the proper API call which we're going to use, and see what type of API call it is, what headers it needs to use, and what endpoint it will be making the request to. In this case, as we need to get all the projects for our account, we can go with the Listing all projects API request. This decision will largely depend on what type of script we want to create, and what data we want to use as a basis.
Once we have this API call selected, we can start creating our script. In this case, I went with the requests
python library:
import requests
Now, we can start setting our header variables. For this API call, I have declared them as such:
# Replace with your DeployHQ API token and email for basic auth
password = "[YOUR_API_TOKEN]"
user = "[YOUR_EMAIL_ADDRESS]"
Then, I declared the URL to which we will be sending the request to:
# API endpoint for listing projects
url = "https://[YOUR_ACCOUNT].deployhq.com/projects"
And finally, the headers themselves, as these are required by the API call declaration:
# Necessary headers
headers = {
"Content-type":"application/json",
"Accept":"application/json"
}
Once done, we can write the API call itself with requests.get()
:
# Send GET request to retrieve projects
response = requests.get(url, auth=(user,password), headers=headers)
So far, this Python script has just set up and ran the DeployHQ native API call, but here's the fun part; now we have retrieved a lot of information in our response
variable, and we can do a lot with it!
In this case, as there's a lot of information in our response body, let's write a simple loop to retrieve the zone for each one of our projects:
if response.status_code == 200:
# Parse JSON data
data = response.json()
# Iterate through projects
for project in data:
# Print each project name
print(f"Project: {project.get("name")}")
# Print each project's zone
print(f"\tZone: {project.get("zone")}")
else:
print(f"Error: API request failed with status code {response.status_code}")
The above code block checks if the API response is successful, and if so, it loops through the information to retrieve what we're interested in.
And we're done! Now, this is a simple example for the sake of presenting the option for scripting, but what you can write and create is limited by your imagination!
You could even expand the above example, and check each project's zone. You could then set up scheduled deployment with our API to deploy your next revision during each zone's off-peak hours.
As mentioned, the sky is the limit with the power of scripting, so go out there and have fun. Happy computing!