Skip to main content

GravixLayer Python SDK

PyPI version Python 3.9+ License: Apache 2.0

Official Python client for GravixLayer. Create isolated cloud runtimes, run code and commands in them, build reusable images, and deploy agents.

pip install gravixlayer
export GRAVIXLAYER_API_KEY="your-api-key"
from gravixlayer import GravixLayer

client = GravixLayer()
runtime = client.runtime.create(template="base-small")

result = runtime.run_code("print('Hello from GravixLayer')")
print(result.text)

runtime.kill()

Cloud and region default to aws / us-east-1. Override with GRAVIXLAYER_CLOUD / GRAVIXLAYER_REGION, or pass them to the client.

Docs: docs.gravixlayer.ai · Examples: examples/

Configuration

from gravixlayer import GravixLayer

client = GravixLayer(
    api_key="your-api-key",          # or GRAVIXLAYER_API_KEY
    base_url="https://api.gravixlayer.ai",
    cloud="aws",
    region="us-east-1",
)
Option Default
api_key GRAVIXLAYER_API_KEY Required.
base_url GRAVIXLAYER_BASE_URL, then https://api.gravixlayer.ai
cloud GRAVIXLAYER_CLOUD, then aws Runtimes and template builds.
region GRAVIXLAYER_REGION, then us-east-1 Runtimes and template builds.
timeout 60 Per request, in seconds.
max_retries 3 Transient failures only.

Construct the client once and reuse it. Call client.warmup() at startup if you want TCP and TLS paid before the first request that matters. HTTP/1.1 is the default; pass http2=True for multiplexing under high concurrency.

Runtimes

A runtime is an isolated virtual machine that boots from a template. It runs until you stop it, or until a timeout you set expires.

runtime = client.runtime.create(
    template="base-small",
    env_vars={"APP_ENV": "staging"},
    timeout=600,
)

Use a context manager when you want it stopped automatically:

from gravixlayer import Runtime

with Runtime.create(template="base-small") as runtime:
    print(runtime.run_code("print(2 + 2)").text)

Code and commands

result = runtime.run_code("print(sum(range(100)))")
print(result.text)

# Pass args when any part comes from user input — nothing in the list is
# interpreted by a shell.
runtime.run_cmd("python", args=["--version"])

Guest egress is deny-by-default. Installing a package or reaching the internet needs a network policy.

Files

runtime.file.write("/workspace/note.txt", "hello\n")
text = runtime.file.read("/workspace/note.txt").content

Also: list, upload, download, write_many, move, copy, find, replace, watch, delete. See examples/runtimes/07_file_operations.py.

State, ports, git, SSH

# Interpreter state that survives between run_code calls.
ctx = client.runtime.create_context(runtime.runtime_id)
client.runtime.run_code(runtime.runtime_id, "x = 1", context_id=ctx.context_id)

# Publish a guest port on https://*.service.gravixlayer.ai
with runtime.service(8000) as api:
    print(api.web_url)
    api.get("/items")

runtime.git.clone("https://github.com/org/repo.git", "/workspace/repo", depth=1)

ssh = runtime.enable_ssh()
print(ssh.connect_cmd)

Templates

Build an image once so runtimes start with everything already installed. Placement follows the client (aws / us-east-1 unless you override it).

from gravixlayer import TemplateBuilder

template = (
    TemplateBuilder("data-science", "Pandas and friends")
    .from_image("python:3.12-slim")
    .vcpu(2)
    .memory(2048)
    .apt_install("git")
    .pip_install("pandas", "matplotlib")
    .start_cmd("python -m http.server 8080")
    .ready_cmd(TemplateBuilder.wait_for_port(8080), timeout_secs=60)
)

status = client.templates.build_and_wait(template)
runtime = client.runtime.create(template=status.template_id)

Snapshots

client.snapshots.create(runtime.runtime_id, "ready-to-work", kind="cold")
restored = client.runtime.create(snapshot="ready-to-work")

A cold snapshot stores the filesystem; a hot snapshot stores memory too, so the restored runtime resumes mid-process.

Agents

agent = client.agents.deploy(source="./my-agent", name="my-agent", is_public=True)
reply = client.agents.invoke(agent.agent_id, input={"prompt": "hello"})

Network policies

A runtime starts fail-closed. Grant access explicitly:

policy = client.network_policies.create(
    name="model-access",
    egress_mode="allowlist",
    rules=[{"destination": "api.example.com", "port": 443, "protocol": "tcp"}],
)

runtime = client.runtime.create(
    template="base-small",
    network_policy_ids=[policy.id],
)

Attaching several policies applies the most restrictive of them, so adding one can only narrow access.

Secrets

provider = client.identity.providers.create(
    "Model API",
    secrets=[{"key": "MODEL_API_KEY", "value": "..."}],
)

runtime = client.runtime.create(
    template="base-small",
    providers=[provider.id],
)

Values are write-only. What comes back is masked.

Async

import asyncio
from gravixlayer import AsyncGravixLayer

async def main():
    async with AsyncGravixLayer() as client:
        runtime = await client.runtime.create(template="base-small")
        result = await client.runtime.run_code(
            runtime.runtime_id, "print('hello')"
        )
        print(result.stdout_text)
        await client.runtime.kill(runtime.runtime_id)

asyncio.run(main())

Errors

from gravixlayer import GravixLayerError, GravixLayerRateLimitError

try:
    client.runtime.create(template="base-small")
except GravixLayerRateLimitError:
    ...
except GravixLayerError as exc:
    print(exc)

Connection failures and 429 / 502 / 503 / 504 are retried automatically.

Examples

Runnable scripts for every surface live in examples/. Start with examples/README.md.

Development

pip install -e ".[test]"
pytest tests/unit_tests

See tests/README.md for layout and live integration tests.

Support

License

Apache License 2.0 — see LICENSE. Copyright 2026 Gravix Layer.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gravixlayer-0.1.84.tar.gz (326.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gravixlayer-0.1.84-py3-none-any.whl (163.3 kB view details)

Uploaded Python 3

File details

Details for the file gravixlayer-0.1.84.tar.gz.

File metadata

  • Download URL: gravixlayer-0.1.84.tar.gz
  • Upload date:
  • Size: 326.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for gravixlayer-0.1.84.tar.gz
Algorithm Hash digest
SHA256 c744c6613b92aef7a5c911f7fd952c9650b88c3bc85e02212d0c76eb51e784b6
MD5 1318f0ae1f23ac22ee52bdc6b34b92aa
BLAKE2b-256 4320800988eb4c8372cc3d4ec1b3928d14d2c01568e930085d9b93564f7c7fe0

See more details on using hashes here.

File details

Details for the file gravixlayer-0.1.84-py3-none-any.whl.

File metadata

  • Download URL: gravixlayer-0.1.84-py3-none-any.whl
  • Upload date:
  • Size: 163.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for gravixlayer-0.1.84-py3-none-any.whl
Algorithm Hash digest
SHA256 5eded7aeb92f7939623c5a338f8cb0673dce376dc45199482aa9416d0974365d
MD5 c1ab38b625191da1d7044b130c0de886
BLAKE2b-256 de1629a06c4b162139d6c52f06f81d0bc0f1035efb4cc46e6987d8f2b469f4f5

See more details on using hashes here.

Release history Release notifications | RSS feed

0.1.96

2 files

0.1.95

2 files

0.1.94

2 files

0.1.93

2 files

0.1.92

2 files

0.1.91

2 files

0.1.90

2 files

0.1.89

2 files

0.1.88

2 files

0.1.87

2 files

0.1.86

2 files

0.1.85

2 files

This release

0.1.84 This release

2 files

0.1.83

2 files

0.1.82

2 files

0.1.81

2 files

0.1.80

2 files

0.1.79

2 files

0.1.78

2 files

0.1.77

2 files

0.1.76

2 files

0.1.75

2 files

0.1.74

2 files

0.1.73

2 files

0.1.72

2 files

0.1.71

2 files

0.1.70

2 files

0.1.69

2 files

0.1.68

2 files

0.1.67

2 files

0.1.66

2 files

0.1.65

2 files

0.1.64

2 files

0.1.63

2 files

0.1.62

2 files

0.1.61

2 files

0.1.60

2 files

0.1.59

2 files

0.1.58

2 files

0.1.57

2 files

0.1.56

2 files

0.1.55

2 files

0.1.54

2 files

0.1.53

2 files

0.1.52

2 files

0.1.51

2 files

0.1.50

2 files

0.1.49

2 files

0.1.48

2 files

0.1.47

2 files

0.1.46

2 files

0.1.45

2 files

0.1.44

2 files

0.1.43

2 files

0.1.42

2 files

0.1.41

2 files

0.1.40

2 files

0.1.39

2 files

0.1.38

2 files

0.1.37

2 files

0.1.36

2 files

0.1.35

2 files

0.1.34

2 files

0.1.33

2 files

0.1.32

2 files

0.1.31

2 files

0.1.30

2 files

0.1.29

2 files

0.1.28

2 files

0.1.27

2 files

0.1.26

2 files

0.1.25

2 files

0.1.24

2 files

0.1.23

2 files

0.1.2

2 files

0.1.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page