How to Use the Linode Python API

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 how to use the official Linode Python API. At this time of this writing, the latest version of the Linode Python API is v4. Source code and documentation is available on GitHub.

Getting Started with the Linode Python API

In order to use the Linode Python API, you will need to generate an API token. You can do this within your Linode dashboard. See the video below for specifically where to do this.

Next, you will need to install the Linode Python package. You can do this via git, but also with pip. Here’s how to install the Linode Python API with the pip package manager.

pip install linode_api4

Linode Python API Example

First, you must import the linode_api4 library and instantiate a LinodeClient object with your API token.

from linode_api4 import LinodeClient

token = "<your-api-token>"
client = LinodeClient(token)

In order to deploy a server instance via the Linode API, you will need 3 pieces of information.

  1. Image: the operating system or image to use
  2. Type: the type of Linode to deploy including CPU and RAM
  3. Region: physical location of the datacenter

Let’s first find out what the valid options are for these three required parameters.

// determine available images
available_images = client.images()
[i for i in available_images]

// determine available types
available_types = client.linode.types()
[i for i in available_types]

// determine available regions
available_regions = client.regions()
[i for i in available_regions]

Feel free to choose any value that meets your needs for image, type, and region.

With this information, we can deploy a Linode VPS with Python.

type = 'g6-nanode-1'
region = 'us-southeast'
image = 'linode/ubuntu20.04'

new_linode, password = client.linode.instance_create(type, region,image=image, label="HellofromPython")

// look at the attributes of the new_linode object to find the IP address
new_linode.__dict__

After your Linode server boots up, you can access it via SSH with the password from above.

YouTube video

Please let me know if you have any questions, and check out some of my other Python tutorials here.


Meet Tony

With a strong software engineering background, Tony is determined to help as many people as possible start their online busines. Discover why Tony quit his hedge fund job to pursue this mission. You can send Tony a message here.

Leave a Comment