LightYear
/Docs
DocsAPI ReferencePython SDK Quickstart

Python SDK Quickstart

Install the LightYear Python SDK and manage your infrastructure with Python.

beginner
6 min read
LightYear Team
Updated April 24, 2026
apipythonsdkautomation

Installation

>_BASH
$pip install lightyear-cloud

Authentication

PYTHON
from lightyear import LightYear

client = LightYear(api_key="your-api-key")
# Or use environment variable: LIGHTYEAR_API_KEY
client = LightYear()

List Servers

PYTHON
servers = client.servers.list()
for server in servers:
    print(f"{server.id}: {server.label} ({server.status}) — {server.main_ip}")

Create a Server

PYTHON
server = client.servers.create(
    region="hkg",
    plan="gpu-a100-80",
    os="ubuntu-22-04",
    label="ml-training-01",
    ssh_key_ids=["key_abc123"],
    backups=True,
)
print(f"Created server: {server.id}")

# Wait for server to become active
import time
while server.status != "active":
    time.sleep(10)
    server = client.servers.get(server.id)
    print(f"Status: {server.status}")

print(f"Server ready at {server.main_ip}")

Stop and Start a Server

PYTHON
# Stop
client.servers.stop("srv_abc123")

# Start
client.servers.start("srv_abc123")

Take a Snapshot

PYTHON
snapshot = client.snapshots.create(
    server_id="srv_abc123",
    description="pre-upgrade-snapshot",
)
print(f"Snapshot: {snapshot.id}")

Manage Firewall Rules

PYTHON
# Create firewall group
group = client.firewall_groups.create(description="web-servers")

# Add rule
rule = client.firewall_rules.create(
    firewall_group_id=group.id,
    protocol="tcp",
    port="443",
    source="0.0.0.0/0",
    action="accept",
)

Error Handling

PYTHON
from lightyear.exceptions import InsufficientBalanceError, NotFoundError

try:
    server = client.servers.create(...)
except InsufficientBalanceError:
    print("Please top up your wallet")
except NotFoundError as e:
    print(f"Resource not found: {e}")

Was this article helpful?

Your cookie choices for this website

This site uses cookies and related technologies, as described in our privacy policy, for purposes that may include site operation, analytics, and enhanced user experience. You may choose to consent to our use of these technologies, or manage your own preferences. Cookie policy