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

# AgentPayClient Class

> Reference for the AgentPayClient class, including constructor parameters and basic usage examples for the Python SDK.

The `AgentPayClient` is the primary interface for interacting with the AgentPay system from your Python MCP server.

<Note>
  The `agentpay-sdk` package currently on PyPI is a **placeholder** to reserve the name during Early Access. To get the actual SDK now, [join the Waitlist](https://agentpay.me).
</Note>

## Initialization

To begin, you need to import and initialize the `AgentPayClient`:

```python theme={null}
from agentpay_sdk import AgentPayClient
import os

# Retrieve your Service Token
# It's highly recommended to store and access your Service Token via an environment variable.
SERVICE_TOKEN = os.getenv("AGENTPAY_SERVICE_TOKEN")

if not SERVICE_TOKEN:
    # Handle the error appropriately in your application context
    print("CRITICAL: AGENTPAY_SERVICE_TOKEN environment variable is not set.")
    # For example, you might raise an exception:
    # raise EnvironmentError("AGENTPAY_SERVICE_TOKEN is not set.")
    agentpay_client = None
else:
    try:
        agentpay_client = AgentPayClient(service_token=SERVICE_TOKEN)
        print("AgentPayClient initialized successfully.")
    except Exception as e:
        print(f"Error initializing AgentPayClient: {e}")
        agentpay_client = None
```

### `AgentPayClient` Constructor

`AgentPayClient(service_token: str)`

* **`service_token` (str, required):**
  * Your unique Service Token obtained from the AgentPay Hub after registering your MCP server.
  * This token authenticates your server with AgentPay.
  * **Security Note:** The Service Token is highly sensitive. It is strongly recommended to load it from an environment variable or a secure secrets management system rather than hardcoding it.

## Singleton Pattern (Recommended)

It's generally recommended to initialize the `AgentPayClient` once and reuse the instance throughout your application. This avoids redundant initializations and can be more efficient. How you achieve this depends on your web framework:

* In Starlette, you might store it on the application object
* In Flask, you might use the application context
* In Django, you might use a custom middleware

## Error Handling

Ensure proper error handling when initializing the client:

```python theme={null}
# Later in your code, before making calls:
if agentpay_client:
    # Proceed with agentpay_client.consume() or agentpay_client.validate_api_key()
else:
    # Handle the case where client initialization failed
```

## Methods

This SDK reference page focuses on the `AgentPayClient` object initialization. For details on its methods, see:

* [Validate API Key Method](./validate-api-key-method)
* [Consume Method](./consume-method)
