This is a quick introduction to the UpCloud API. It will help you through the first steps of connecting to the API and automating your cloud servers using your favourite programming language. The programmable API is available at https://api.upcloud.com, which encompasses all of the functionality of the UpCloud control panel and then some; if you want to get started on your own, head straight to the full API documentation.
Creating API credentials
The first step in getting started with our API is to create a separate API user. You can do this at the UpCloud Control Panel using a subaccount. Your API account name and password are very similar to an API ID and key pair, with the added benefit of being able to set them yourself.
Create a new subaccount for the API access by clicking the Create subaccount button.
Then, fill in the necessary details:
- Choose your API username
- Set the API password
- Enter the contact details for your API credentials
- When set, click the Create subaccount button to save
Next, go to the Permissions tab and enable API access in the permissions
Grant the permissions to the resources you want the API to have access to
By default, the API account will have full permissions to any resources it creates but no access to existing servers or storage. You can grant additional permissions using the server, storage, and tag access options.
You can create as many dedicated API accounts as you need. We recommend creating separate API credentials for each application and external service you use.
Note that you should restrict the API subaccount from your other UpCloud services. If you are programming automation against the UpCloud API, take care handling the credentials.
Running basic API requests
Once you have created your API account, let’s try your first request. To quickly test the API, use a tool such as Postman (or any other API toolkit of your choice) to get started. The goal is to have the following dialogue working.
GET /1.3/account
HTTP/1.0 200 OK { "account" : { "credits" : "10000", "username" : "username" } }
Using a GET request, enter the API address https://api.upcloud.com with the API version number and the desired command to the request URL line. Then, select the Basic Auth option and set your username and password. After filling in the request details, click the Send button to run the query.
The reply will be shown in the bottom half of the Postman window under the Body tab.
The authentication method of our API is HTTP Basic authentication, where the Authorization header should contain your API username and Base64 encoded password. More precisely:
# Python3 import base64 base64.b64encode("username:password".encode())
# Node.js new Buffer("username:password").toString('base64');
Postman’s authorization header line should look as shown below with your Base64 encoded credentials.
Using a similar request by just replacing the query target from account to server you can get the full list of servers your API account is allowed to access.
GET /1.3/server
HTTP/1.0 200 OK { "servers": { "server": [ { "core_number": "1", "hostname": "example.upcloud.com", "license": 0, "memory_amount": "1024", "plan": "1xCPU-1GB", "state": "started", "tags": { "tag": [] }, "title": "Example UpCloud server", "uuid": "00e8051f-86af-468b-b932-4fe4ac6c7f08", "zone": "fi-hel1" } ] } }
You should now be able to form similar requests to the ones above using the API. For example, GET /1.3/server/00e8051f-86af-468b-b932-4fe4ac6c7f08, will reply with full details of that specific host when you include the UUID of one of your servers in a query.
Using the API programmatically
The API can be accessed using any language that has proper HTTP libraries. The following snippet shows how you could take advantage of the UpCloud API using Python3.
import http.client import base64 conn = http.client.HTTPSConnection("api.upcloud.com") auth = base64.b64encode("username:password".encode()) headers = {"Authorization": "Basic " + auth.decode()} conn.request("GET", "/1.3/account", None, headers) res = conn.getresponse() print( res.read().decode() )
For a bit more sensible approach, the example below shows how you could structure the code for a better approach. The BaseAPI forms a generic API (GET) request extended by the Account class to form the same API request as above. Adding additional GET requests would now be much easier.
import http.client import base64 class BaseAPI: api = "api.upcloud.com" api_v = "1.3" token = base64.b64encode("username:password".encode()) ''' Performs a GET request to a given endpoint in UpCloud's API. ''' def get(self, endpoint): conn = http.client.HTTPSConnection(self.api) url = "/" + self.api_v + endpoint headers = { "Authorization": "Basic " + self.token.decode(), "Content-Type": "application/json" } conn.request("GET", url, None, headers) res = conn.getresponse() self.printresponse(res.read()) ''' Prints the response (bytes) as a string to the user ''' def printresponse(self, res): data = res.decode(encoding="UTF-8") print(data) class Account(BaseAPI): endpoint="/account" def do(self): self.get(self.endpoint) if __name__ == "__main__": Account().do()
More about the UpCloud API
These are just some of the simplest examples of what the UpCloud API allows you to do. Now that you have mastered the API usage, proceed to deploy a new server with the UpCloud API.
Soha Zain
Amazingly Great job. These two points are well covered; “Creating API credentials” and “Using the API programmatically”. Thanks for sharing this topic “Getting started using the UpCloud API”. The best part is the article has all the practical detailing! Keep sharing