> ## Documentation Index
> Fetch the complete documentation index at: https://docs.zerogpu.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Python

> Integrate ZeroGPU into your Python application.

## Official SDK (recommended)

Install the published client from [PyPI](https://pypi.org/project/zerogpu-api/):

```bash theme={null}
pip install zerogpu-api
```

```python theme={null}
import os
from zerogpu import ZerogpuApi

client = ZerogpuApi(
    api_key=os.environ["ZEROGPU_API_KEY"],
    project_id=os.environ["ZEROGPU_PROJECT_ID"],
)

# Many models accept plain string `input`; others accept a message list: see API reference.
response = client.responses.create_response(
    model=os.environ.get("ZEROGPU_MODEL", "YOUR_MODEL"),
    input="Your input text here...",
    text={"format": {"type": "text"}},
)
print(response)
```

Source and regeneration workflow: [zerogpu/SDK](https://github.com/zerogpu/SDK) on GitHub.

### Chat completions (`/v1/chat/completions`)

For models that use the chat route:

```python theme={null}
from zerogpu import ChatMessage

client.chat.create_chat_completion(
    model="YOUR_MODEL",
    messages=[ChatMessage(role="user", content="Hello")],
)
```

See [Chat completions](/api-reference/chat-completions).

### Responses with `metadata`

Pass `metadata={...}` into `create_response` when the model supports it. See [Responses](/api-reference/responses).

***

## Raw HTTP (`requests`)

If you prefer not to use the SDK, call the endpoint directly.

```python theme={null}
import requests

url = "https://api.zerogpu.ai/v1/responses"
headers = {
    "content-type": "application/json",
    "x-api-key": "YOUR_API_KEY",
    "x-project-id": "YOUR_PROJECT_ID",
}
payload = {
    "model": "YOUR_MODEL",
    "input": [
        {
            "role": "user",
            "content": "Your input text here...",
        }
    ],
    "text": {
        "format": {
            "type": "text"
        }
    },
}

response = requests.post(url, headers=headers, json=payload)
print(response.json())
```

## Using environment variables

```python theme={null}
import os
import requests

url = "https://api.zerogpu.ai/v1/responses"
headers = {
    "content-type": "application/json",
    "x-api-key": os.environ["ZEROGPU_API_KEY"],
    "x-project-id": os.environ["ZEROGPU_PROJECT_ID"],
}
payload = {
    "model": "YOUR_MODEL",
    "input": [
        {
            "role": "user",
            "content": "Your input text here...",
        }
    ],
    "text": {
        "format": {
            "type": "text"
        }
    },
}

response = requests.post(url, headers=headers, json=payload)
result = response.json()

print(result["output"][0]["content"][0]["text"])
```

## Error handling

```python theme={null}
import requests

try:
    response = requests.post(url, headers=headers, json=payload)
    response.raise_for_status()
    result = response.json()
    print(result["output"][0]["content"][0]["text"])
except requests.exceptions.HTTPError as e:
    if e.response.status_code == 401:
        print("Invalid API key")
    else:
        print(f"Error: {e.response.status_code}")
except requests.exceptions.RequestException as e:
    print(f"Request failed: {e}")
```

## Install dependencies

```bash theme={null}
pip install requests
```
