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

# Python SDK

> Read a profile, connect users, and store OAuth tokens safely.

Use Python 3.10 or later on your backend (your app's server). Get an access token with the `profile:read` permission and a linked account ID from [Connect an account](https://docs.ofauth.com/v3/guides/link.md).

## Install the release candidate

```bash theme={null}
python -m pip install "onlyfans-sdk==4.0.0rc1"
```

## Read the current profile

Set `OFAUTH_ACCESS_TOKEN` and `OFAUTH_LINKED_ACCOUNT_ID` in your server's environment. Save this as `profile.py` and run `python profile.py`:

```python theme={null}
import os
from onlyfans_sdk import OFAuthClient

with OFAuthClient(
	access_token=os.environ["OFAUTH_ACCESS_TOKEN"],
	linked_account_id=os.environ["OFAUTH_LINKED_ACCOUNT_ID"],
) as client:
	profile = client.access.profile.get()
	print(profile["id"], profile["username"], profile["name"])
```

The SDK sends the access token and linked account header for you. The `with` block closes the client when you finish.

## Check the result

The program prints the account ID, username, and name. Missing JSON values appear as Python `None`. This request does not change the account.

If the API returns an error, the SDK raises `OFAuthAPIError`. Read its `code`, `recovery_action`, and `request_id`. Follow [Handle failed requests](https://docs.ofauth.com/v3/reference/error-handling.md) to choose the next step. Check whether a message or post was created before repeating a request whose result you did not receive.

## Use the client in your application

Use `iterate()` to read all pages of a list. Inside the same `with` block, list the accounts shared through this authorization:

```python theme={null}
for account in client.linked_accounts.iterate():
	print(account["id"], account["display_name"])
```

Use `client.with_linked_account(account_id)` to select another account shared through the same authorization. For async code, use `AsyncOFAuthClient` with `async with` and await its requests.

## Connect customers and renew access

Use the SDK's OAuth helper or a standard confidential OAuth client with the [authorization-code contract](https://docs.ofauth.com/v3/api-reference/link/authorize.md). Persist callback state and PKCE against the initiating application user and browser session, with expiry and single use. Validate the issuer and exact redirect URI before exchange.

Your auth framework or application owns token persistence and refresh. Supply its current access token to the resource client. Use the returned `expires_in`, and refresh through the form-encoded [token endpoint](https://docs.ofauth.com/v3/api-reference/link/tokens.md). Code exchanges always return a refresh token.

The refresh token is stable and bound to the authenticated client. Independent instances can refresh concurrently; retry a lost refresh response with the same token. No database refresh lock, attempt marker, or replay journal is required. Use ordinary conditional writes so an earlier refresh cannot replace a registration the customer has changed or disconnected.

Keep client and environment in each application's customer storage key. Discover all granted accounts, then persist an explicit account choice. An account ID does not choose authority or billing.

Revoking a refresh token ends that credential and its access tokens. Ending the authorization ends all its credentials. A provider session error requires [account reconnect](https://docs.ofauth.com/v3/guides/reconnect.md), which does not renew OAuth or replay a write. See the [Express source example](https://github.com/ofauth-source/example-express-oauth) for the complete backend journey.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.