Installation
>_BASH
$pip install lightyear-cloudAuthentication
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}")