> ## 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.

# Call the OnlyFans API from Python

> Connect accounts and call OnlyFans data with the version 4 Python SDK.

Use the Python SDK release candidate on your server. The account owner approves the accounts and permissions your app requests. Your app then calls the API for a selected linked account.

## Install the SDK

Use Python 3.10 or later:

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

## Connect a signed-in user

Use a standard confidential OAuth adapter or the SDK’s OAuth helper. Register the exact backend callback and use S256 PKCE. Keep callback state, verifier, initiating application user, session, expiry, and registration version in your application’s ordinary persistence.

Before exchange, atomically consume the state for the same trusted user and session. A foreign callback must not consume it. See [Connect customers and renew access](https://docs.ofauth.com/v3/sdk/python.md#connect-customers-and-renew-access) and the complete [Express example](https://github.com/ofauth-source/example-express-oauth).

## Store and refresh tokens

Your auth framework or application owns persistence and supplies the current access token to the resource SDK. Stable client-bound refresh tokens permit concurrent refresh and retries after a lost response, without a refresh lock or attempt marker. Use conditional writes when the customer replaces or disconnects a registration.

Keep client and environment in each customer’s storage key. An invalid refresh grant requires authorization again; temporary storage or network failures can be retried.

## Choose an account and read its profile

List all shared accounts with `client.linked_accounts.iterate()` and let the user choose. Save the selected ID under the same client and user key. Verify the saved choice against the current shared accounts before use.

The client accepts your application’s token getter and selected account:

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

with OFAuthClient(
	access_token=lambda force_refresh=False: access_token(user_id, force_refresh),
	linked_account_id=selected_account_id,
) as client:
	profile = client.access.profile.get()
	print(profile["id"], profile["username"], profile["name"])
```

`access_token` is your application function that returns its current access token. `user_id` comes from your app session, and `selected_account_id` is the user’s verified choice. Do not automatically use the first account returned.

For async code, use `AsyncOFAuthClient` with `async with` and await its requests.

## Handle account recovery

An expired OnlyFans session needs an account reconnect, while a revoked authorization needs fresh approval. Follow [Handle account reconnects](https://docs.ofauth.com/v3/guides/reconnect.md) and [Handle failed requests](https://docs.ofauth.com/v3/reference/error-handling.md). Check whether a post or message completed before you repeat a request whose result was lost.

For the full flow in TypeScript, see the [Express OAuth source example](https://github.com/ofauth-source/example-express-oauth).


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