How to Use the Vultr API with Python

by

Hey there! Some links on this page may be affiliate links which means that, if you choose to make a purchase, I may earn a small commission at no extra cost to you. I greatly appreciate your support!

In this tutorial, you will learn basic Vultr API v2 usage with the curl command and also with Python. By the end of this tutorial, you will know how to deploy a Vultr VPS from via the API and get it’s IP address.

Vultr API with Curl

In order to deploy a Vultr server, you need three pieces of information.

  1. Region
  2. Plan
  3. Operating system

Let’s determine the possible values for each of these, and choose one that meets our needs.

First, let’s determine the valid regions.

curl --location --request GET 'https://api.vultr.com/v2/regions'

The curl command will return a dictionary with all the possible regions. In this case, let’s choose syd for the datacenter in Sydney.

Next, let’s figure out what the options are for plans.

curl --location --request GET 'https://api.vultr.com/v2/plans'

The curl command will return a dictionary with all the possible Vultr plans. Let’s choose a basic vc2-1c-1gb instance.

Finally, let’s find out which operating systems are available.

curl --location --request GET 'https://api.vultr.com/v2/os'

The curl command will return a dictionary with all the possible operating systems. I want Ubuntu 20.04 so theĀ  id associated with that is 387.

With this information, we can create spin up a Vultr server with the curl command.

curl --location --request POST 'https://api.vultr.com/v2/instances' \
--header 'Authorization: Bearer <your-api-key>' \
--header 'Content-Type: application/json' \
--data-raw '{
    "region" : "syd", 
    "plan" : "vc2-1c-1gb", 
    "label" : "Hello from Curl",
    "os_id" : 387
}'

Feel free to choose your own values for region, plan, label, and os_id. Also, replace <your-api-key> with your actual Vultr API key.

The output from the curl command will contain information about your new server instance. Please take note of the instance id. We will use this next.

With your instance id, query the Vultr API again for information about your instance.

curl --location --request GET 'https://api.vultr.com/v2/instances/<your-instance-id>' \
--header 'Authorization: Bearer <your-api-key>'

In the output, you will find the main_id field with the value of your instance’s IP address.

Vultr API with Python

We can use Python to interact with the Vultr API. To do this, we will primarily be using the requests Python library.

Based on the information from above, here is a full Python script that creates a Vultr VPS and gets it’s IP address.

import time
import requests

# private constants
api_key = "<your-api-key>"

# api url
url = "https://api.vultr.com/v2/instances"

headers = {"Authorization": "Bearer {}".format(api_key),
           "Content-Type": "application/json"}
data = {
    "region" : "ewr", 
    "plan" : "vc2-1c-1gb", 
    "label" : "Hello from Python",
    "os_id" : 387
}
response = requests.post(url, json=data, headers=headers)
instance_id = response.json()['instance']['id']

# wait a minute until the VPS is ready
time.sleep(60)

# get instance ip address
instance_url = "{}/{}".format(url, instance_id)
instance_response = requests.get(instance_url, headers=headers)
ip_address = instance_response.json()['instance']['main_ip']

For more context, here is a video tutorial about interacting with the Vultr API that I created.

YouTube video

In case you still have questions, please don’t hesitate to leave a comment below or on the video. Find more Python tutorials here!


Meet Tony

With a background in computer science and engineering, Tony is determined to demystify the web. Discover why Tony is pursuing this mission. You can also connect with Tony here.

Leave a Comment