Python SDK
Use the ServersCamp Python SDK to manage your infrastructure programmatically.
Installation
Bash
pip install serverscamp
Authentication
Get your API token from your ServersCamp console.
Python
import serverscamp
# Option 1: Set environment variable
# export SCAMP_API_TOKEN=your-token
# Option 2: Pass token directly
client = serverscamp.Client(token="your-api-token")
# Option 3: Use token from env (default)
client = serverscamp.Client()
Quick start examples
List all VMs
Python
from serverscamp import Client
client = Client()
# List all VMs
vms = client.vms.list()
for vm in vms:
print(f"{vm.name}: {vm.ip_address} ({vm.status})")
Create a VM
Python
from serverscamp import Client
client = Client()
# Create a new VM
vm = client.vms.create(
name="web-server",
region="eu-central-1",
image="ubuntu-22.04",
size="burst-xs",
ssh_keys=["my-key"],
storage={
"size_gb": 50,
"class": "standard"
}
)
print(f"Created VM: {vm.id}")
# Wait for VM to be ready
vm.wait_until_running()
print(f"VM is running at {vm.ip_address}")
Create a VPC with subnet
Python
from serverscamp import Client
client = Client()
# Create VPC
vpc = client.vpcs.create(
name="production-vpc",
region="eu-central-1",
cidr_block="10.0.0.0/16"
)
# Create subnet
subnet = client.subnets.create(
name="web-subnet",
vpc_id=vpc.id,
cidr_block="10.0.1.0/24",
availability_zone="eu-central-1a"
)
print(f"VPC: {vpc.id}, Subnet: {subnet.id}")
Manage databases
Python
from serverscamp import Client
client = Client()
# Create PostgreSQL database
db = client.databases.create(
name="app-db",
engine="postgres",
version="15",
size="db-small",
region="eu-central-1",
vpc_id="vpc-123"
)
# Wait for database to be ready
db.wait_until_available()
# Get connection string
print(f"Host: {db.host}")
print(f"Port: {db.port}")
print(f"Connection: {db.connection_string}")
Complete example: full stack setup
Python
from serverscamp import Client
def setup_production_stack():
client = Client()
# 1. Create VPC
vpc = client.vpcs.create(
name="prod-vpc",
region="eu-central-1",
cidr_block="10.0.0.0/16"
)
# 2. Create subnets
web_subnet = client.subnets.create(
name="web-subnet",
vpc_id=vpc.id,
cidr_block="10.0.1.0/24"
)
db_subnet = client.subnets.create(
name="db-subnet",
vpc_id=vpc.id,
cidr_block="10.0.2.0/24"
)
# 3. Create database
db = client.databases.create(
name="prod-db",
engine="postgres",
version="15",
size="db-medium",
region="eu-central-1",
vpc_id=vpc.id,
subnet_id=db_subnet.id
)
# 4. Create web servers
web_servers = []
for i in range(2):
vm = client.vms.create(
name=f"web-{i+1}",
region="eu-central-1",
image="ubuntu-22.04",
size="standard",
vpc_id=vpc.id,
subnet_id=web_subnet.id,
ssh_keys=["prod-key"],
user_data=f"""#!/bin/bash
apt-get update
apt-get install -y nginx
echo "Server {i+1}" > /var/www/html/index.html
"""
)
web_servers.append(vm)
# 5. Create load balancer
lb = client.load_balancers.create(
name="prod-lb",
region="eu-central-1",
vpc_id=vpc.id,
targets=[vm.id for vm in web_servers],
health_check={
"path": "/health",
"interval": 30
}
)
# Wait for everything
db.wait_until_available()
for vm in web_servers:
vm.wait_until_running()
print(f"Load Balancer URL: {lb.dns_name}")
print(f"Database Host: {db.host}")
return {
"vpc": vpc,
"lb": lb,
"db": db,
"servers": web_servers
}
if __name__ == "__main__":
stack = setup_production_stack()
Error handling
Python
from serverscamp import Client
from serverscamp.exceptions import (
AuthenticationError,
NotFoundError,
RateLimitError,
ValidationError
)
client = Client()
try:
vm = client.vms.get("vm-invalid")
except NotFoundError:
print("VM not found")
except AuthenticationError:
print("Invalid API token")
except RateLimitError as e:
print(f"Rate limited, retry after {e.retry_after}s")
except ValidationError as e:
print(f"Invalid request: {e.message}")
Async support
The SDK also provides an async client for use with asyncio:
Python
import asyncio
from serverscamp import AsyncClient
async def main():
client = AsyncClient()
# Create multiple VMs concurrently
tasks = [
client.vms.create(name=f"worker-{i}", ...)
for i in range(5)
]
vms = await asyncio.gather(*tasks)
print(f"Created {len(vms)} VMs")
asyncio.run(main())