We offer a programmable API at https://api.upcloud.com, that encompasses all the functionality of our Control Panel and then some.

The first step in getting started with our API is to create an API user. You can do this at the Control Panel’s My Account – Usernames view. We strongly recommend limiting the origin of allowed contacts (the “Allow contacts from”-field) for security reasons.

Warning: If someone get’s access to your API they have total control over your account – be sure to handle credentials properly if you are programming automations against our API.

The First API Request

After you have created the API account, let’s try to get your first request working. The goal is to have the following dialogue working.

GET /1.1/account
HTTP/1.0 200 OK
{
    "account" : {
        "credits" : "10000",
        "username" : "username"
    }
}

To quickly test the API, use a tool such as Postman to (or any other tool of your choice) to get started. The authentication method of our API is HTTP Basic authentication where the Authorization header should contain your API username and password base64 encoded. More precisely:

# Python3
import base64
base64.b64encode("username:password".encode())

# Node.js
new Buffer("username:password").toString('base64');

You should now be able to form a similar request to the following, where base64() should be replaced with your encoded credentials.

Postman_APIcall

Programmatically using the API

The API can be accessed by any language that has proper HTTP libraries; the following snippet shows how you could access our API from Python3.

import http.client

conn = http.client.HTTPSConnection("api.upcloud.com")
conn.request("GET", "/1.1/account", None, {"Authorization": "Basic base64_encoded_credentials"})
res = conn.getresponse()
print( res.read() )

For a bit more sensible approach, the following shows how you could structure the code for a better approach. The BaseAPI forms a generic API (GET) request and it is 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 json

class BaseAPI:
	api = 'api.upcloud.com'
	api_v = '1.1'
	token = 'Basic base64_encoded_credentials'

	'''
	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": self.token, 
			"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(json.loads(data))

class Account(BaseAPI):
	endpoint='/account'

	def do(self):
		self.get(self.endpoint)

More

Take a look at the API Documentation!