# Python SDK

This guide explains how to interact with the API using the Python client.

***

## Installation

```bash
pip install fortisx
```

or, if you prefer a local install:

```bash
pip install -e .
```

***

## Initialization

```python
from fortisx import API

api = API(api_key="YOUR_API_KEY")
```

**Constructor options**

| Name       | Type | Default                     | Description                            |
| ---------- | ---- | --------------------------- | -------------------------------------- |
| `api_key`  | str  | –                           | API key used for authorization         |
| `base_url` | str  | `https://api.fortisx.fi/v1` | Override if using a custom environment |
| `timeout`  | int  | `10`                        | Request timeout in seconds             |

***

## Methods

| Method                       | Arguments      | Returns | Description               |
| ---------------------------- | -------------- | ------- | ------------------------- |
| `get(endpoint, params=None)` | `str`, `dict?` | `dict`  | Performs a GET request    |
| `post(endpoint, data=None)`  | `str`, `dict?` | `dict`  | Performs a POST request   |
| `put(endpoint, data=None)`   | `str`, `dict?` | `dict`  | Performs a PUT request    |
| `delete(endpoint)`           | `str`          | `dict`  | Performs a DELETE request |

> All methods automatically include the following headers:
>
> ```python
> {
>   "Authorization": "Bearer {api_key}",
>   "Accept": "application/json"
> }
> ```

***

## Error Handling

When a network or server error occurs, an exception of type `APIError` is raised.

```python
class APIError(Exception):
    def __init__(self, message, status=None, details=None):
        super().__init__(message)
        self.status = status
        self.details = details
```

| Field     | Type | Description                     |
| --------- | ---- | ------------------------------- |
| `message` | str  | Short description of the error  |
| `status`  | int  | HTTP status code                |
| `details` | dict | Full response body if available |

**Example:**

```python
from fortisx import API, APIError

api = API("demo-key")

try:
    res = api.get("ping")
    print(res)
except APIError as err:
    print(f"API error [{err.status}]: {err}")
except Exception as e:
    print("Unexpected error:", e)
```

***

## Example: `/ping` Endpoint

```python
from fortisx import API

api = API("demo-key")

res = api.get("ping")
print(res)  # {'status': 'ok'}
```
