Class Initialization
Class Initialization is the first step in integrating the PhonePe Python SDK into your application. In this step, you’ll set up the necessary classes and configurations to begin interacting with the SDK’s functionalities.
SubscriptionClient Class Configuration
The SubscriptionClient class is used for communication with PhonePe APIs. It must be initialized only once, and the required credentials need to be provided during the initialization process.
⚠️ Do not store credentials in production code!
For production builds, do not store credentials directly in the code.
Parameters for Initialization
| Parameter Name | Data Type | Mandatory (Yes/No) | Default Value | Description |
| client_id | String | Yes | Your unique client ID for secure communication with PhonePe. | |
| client_version | Integer | Yes | Client version for secure communication with PhonePe. | |
| client_secret | String | Yes | Secret key provided by PhonePe Payment Gateway, which must be securely stored. | |
| env | Enum | Yes | Environment for the client: • PRODUCTION • SANDBOX | |
| pool_size | int | No | 10 | Max pooled (kept-alive) connections per host. Low-traffic merchants can use 2–4; high-concurrency merchants should raise it. |
| keep_alive_seconds | float | No | 60 | A pooled connection idle longer than this is closed and replaced before it is reused, so a connection silently dropped by the network is never handed to a request. |
| connect_timeout_seconds | float | No | 3 | Max time to establish the TCP/TLS connection. |
| read_timeout_seconds | float | No | 30 | Max time to wait for a response after the request is sent. |
| pool_block | bool | No | TRUE | When all pooled connections are busy, wait for one to free up instead of opening a throwaway connection outside the pool. |
PhonePeException Handling
PhonePeException will be thrown if a new SubscriptionClient instance is created with different credentials than the previous instance. Ensure that credentials remain consistent to avoid this error.
Example Usage
from phonepe.sdk.pg.common.configs.http_client_config import HttpClientConfig
from phonepe.sdk.pg.payments.v2.subscription_client import SubscriptionClient
from phonepe.sdk.pg.env import Env
http_client_config = HttpClientConfig(
pool_size=10, # max pooled (kept-alive) connections per host
keep_alive_seconds=60, # proactively recycle connections idle longer than this
connect_timeout_seconds=3, # max time to establish the TCP/TLS connection
read_timeout_seconds=30, # max time to wait for a response once the request is sent
pool_block=True, # wait for a free pooled connection instead of overflowing the pool
)
client = SubscriptionClient.get_instance(
client_id=client_id,
client_secret=client_secret,
client_version=client_version,
env=env,
http_client_config=http_client_config, # optional
)What’s Next?
Now that we’ve walked through class initialization, let’s move on to setting up the subscription.
Head over to the next section and let’s walk you through the process.