# Python SDK

Follow these steps to get started with the Suger Python SDK.

## Step 1: Install the SDK
To include the Suger SDK in your project, you can use pip in your terminal:
```bash
pip install suger-sdk-python
```

## Step 2: Get an OAuth access token

The Suger API uses the OAuth 2.0 client-credentials flow. First, create an
[OAuth App](https://doc.suger.io/get-started/oauth-app/) in your organization
settings to get a **Client ID** and **Client Secret**. Then exchange them for a
short-lived (1 hour) bearer token:

```python

# Exchange OAuth App credentials for a bearer token. Cache it and request a new
# one when it expires (default lifetime: 1 hour).
def get_access_token(client_id: str, client_secret: str) -> str:
    resp = requests.post(
        "https://apiv2.suger.cloud/oauth2/token",
        data={
            "grant_type": "client_credentials",
            "client_id": client_id,
            "client_secret": client_secret,
            "resource": "https://api.suger.cloud",
        },
    )
    resp.raise_for_status()
    return resp.json()["access_token"]
```

The `resource=https://api.suger.cloud` parameter is required — it scopes the
issued JWT to the Suger API. See [API Access](https://doc.suger.io/get-started/oauth-app/)
for the full token exchange, caching, and rotation details.

## Step 3: Set up your configuration and Create ApiClient
Create a configuration and attach the bearer token. `APIKeyAuth` writes the
`Authorization` header verbatim, so pass the OAuth token as `Bearer <token>`:

```python
from suger_sdk_python import Configuration

access_token = get_access_token("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET")
config = suger_sdk_python.Configuration(
    host="https://api.suger.cloud",
)
config.api_key['APIKeyAuth'] = 'Bearer ' + access_token
api_client = suger_sdk_python.ApiClient(config)
```

## Step 4: Use the SDK
Now that you have your client set up, you can call Suger services. Here’s a simple example of how to make a service call:
```python
org_id = 'your_org_id'
offer_id = 'your_offer_id'
try:
    result = OfferApi(api_client).get_offer(org_id, offer_id)
    print("The response of OfferApi:\n")
    print(result)
except ApiException as e:
    print("Exception when calling OfferApi->get_offer: %s\n" % e)
```

## Legacy authentication (API key)

:::warning
API-key authentication is **deprecated**. New integrations should use the OAuth
access token shown above. The API-key example below is retained only for
existing integrations.
:::

To authenticate an existing integration with an API key, pass it as `Key <key>`
instead of a bearer token:

```python
config.api_key['APIKeyAuth'] = 'Key ' + 'your_api_key'
```

## Conclusion
You are now ready to use the Suger Python SDK in your application! For more detailed information and advanced usage, please refer to the official documentation.
