# Python SDK Quickstart

{% hint style="success" %}
**What you'll build:** In 5 minutes, you'll search the GPU marketplace, rent a server, and connect via SSH — all from code or CLI.
{% endhint %}

## Prerequisites

* **Python 3.9+** installed
* **Clore.ai account** — [sign up here](https://clore.ai)
* **API key** — get it from your [Clore.ai dashboard](https://clore.ai)
* **Funds** — minimum \~$5 in BTC, CLORE, USDT, or USDC

***

## Step 1: Install the SDK

```bash
pip install clore-ai
```

Verify installation:

```bash
clore --version
```

***

## Step 2: Configure Your API Key

**Option A: Environment variable (recommended)**

```bash
export CLORE_API_KEY=your_api_key_here
```

**Option B: CLI config (persisted to `~/.clore/config.json`)**

```bash
clore config set api_key YOUR_API_KEY
```

***

## Step 3: Search for a GPU

### CLI

```bash
clore search --gpu "RTX 4090" --max-price 5.0 --sort price --limit 10
```

You'll see a table with server IDs, GPU specs, RAM, price, and location.

### Python

```python
from clore_ai import CloreAI

client = CloreAI()

servers = client.marketplace(gpu="RTX 4090", max_price_usd=5.0)
for s in servers[:5]:
    print(f"Server {s.id}: {s.gpu_count}x {s.gpu_model} — ${s.price_usd:.4f}/h — {s.location}")
```

**Output:**

```
Server 142: 1x NVIDIA GeForce RTX 4090 — $0.0800/h — EU
Server 305: 1x NVIDIA GeForce RTX 4090 — $0.0950/h — US
Server 891: 2x NVIDIA GeForce RTX 4090 — $0.1200/h — EU
```

{% hint style="info" %}
**Tip:** The `marketplace()` method filters client-side. You can also filter by `min_gpu_count`, `min_ram_gb`, and `available_only` (default: `True`).
{% endhint %}

***

## Step 4: Deploy (Rent a Server)

### CLI

```bash
clore deploy 142 \
  --image cloreai/ubuntu22.04-cuda12 \
  --type on-demand \
  --currency bitcoin \
  --ssh-password MySecurePass123 \
  --port 22:tcp \
  --port 8888:http
```

### Python

```python
order = client.create_order(
    server_id=142,
    image="cloreai/ubuntu22.04-cuda12",
    type="on-demand",
    currency="bitcoin",
    ssh_password="MySecurePass123",
    ports={"22": "tcp", "8888": "http"}
)

print(f"Order created! ID: {order.id}")
print(f"IP: {order.pub_cluster}")
print(f"Ports: {order.tcp_ports}")
```

{% hint style="warning" %}
**First boot takes 1–5 minutes.** The server pulls the Docker image and starts services. If `pub_cluster` is `None`, wait and check again with `my_orders()`.
{% endhint %}

***

## Step 5: Connect via SSH

### CLI (auto-connects)

```bash
clore ssh 38
```

The CLI looks up the order, finds the public IP and SSH port, and runs `ssh` for you.

### Manual SSH

```bash
ssh root@<pub_cluster> -p <ssh_port>
# Password: MySecurePass123
```

***

## Step 6: Cleanup

When you're done, cancel the order to stop billing:

### CLI

```bash
clore cancel 38
```

### Python

```python
client.cancel_order(order_id=38, issue="Done with my work")
print("Order cancelled")
```

***

## Full Script: Search → Deploy → Monitor → Cancel

```python
from clore_ai import CloreAI
import time

client = CloreAI()  # Uses CLORE_API_KEY env var

# 1. Find cheapest RTX 4090
servers = client.marketplace(gpu="RTX 4090", max_price_usd=5.0)
servers.sort(key=lambda s: s.price_usd or float("inf"))

if not servers:
    print("No RTX 4090 available under $5/h")
    exit(1)

best = servers[0]
print(f"Best deal: Server {best.id} — ${best.price_usd:.4f}/h — {best.location}")

# 2. Deploy
order = client.create_order(
    server_id=best.id,
    image="cloreai/ubuntu22.04-cuda12",
    type="on-demand",
    currency="bitcoin",
    ssh_password="MySecurePass123",
    ports={"22": "tcp"}
)
print(f"Order {order.id} created!")

# 3. Wait for IP assignment
for _ in range(12):
    orders = client.my_orders()
    current = next((o for o in orders if o.id == order.id), None)
    if current and current.pub_cluster:
        print(f"Ready! SSH: ssh root@{current.pub_cluster} -p {current.tcp_ports.get('22', 22)}")
        break
    print("Waiting for server to start...")
    time.sleep(10)

# 4. ... do your work ...

# 5. Cancel when done
client.cancel_order(order_id=order.id)
print("Order cancelled, billing stopped.")
```

***

## What's Next

| Guide                                                                         | What You'll Learn                                                |
| ----------------------------------------------------------------------------- | ---------------------------------------------------------------- |
| [Python SDK Guide](https://docs.clore.ai/guides/advanced/python-sdk)          | Async operations, spot market, server management, error handling |
| [CLI Automation](https://docs.clore.ai/guides/advanced/cli-automation)        | Bash scripts, CI/CD integration, batch deploy                    |
| [API Integration](https://docs.clore.ai/guides/advanced/api-integration)      | Connect AI services running on Clore to your apps                |
| [GPU Comparison](https://docs.clore.ai/guides/getting-started/gpu-comparison) | Choose the right GPU for your workload                           |

***

**Happy GPU renting! 🚀**
