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

# TypeScript SDK

> Use supplied access tokens and explicitly select linked accounts.

The SDK is a resource client. Your auth framework or application owns OAuth callbacks, token persistence, refresh, and revocation.

## Install the release candidate

```bash theme={null}
npm install @ofauth/onlyfans-sdk@4.0.0-rc.1
```

The `next` tag tracks the current prerelease.

```bash theme={null}
npm install @ofauth/onlyfans-sdk@next
```

## Read a profile

```typescript theme={null}
import { OFAuthClient } from "@ofauth/onlyfans-sdk"

const client = new OFAuthClient({
	accessToken: process.env.OFAUTH_ACCESS_TOKEN!,
	linkedAccountId: process.env.OFAUTH_LINKED_ACCOUNT_ID!
})
const profile = await client.access.profile.get()
```

A supplied token getter can obtain the current access token from your auth system:

```typescript theme={null}
const client = new OFAuthClient({
	accessToken: async () => currentAccessToken(),
	linkedAccountId: selectedLinkedAccountId
})
```

`currentAccessToken` is your application function. If you use [Better Auth](https://docs.ofauth.com/v3/sdk/better-auth.md), obtain it through Better Auth's token API. Keep one token owner. Constructing the resource client performs no compulsory account preflight.

## Choose an account

Create a discovery client with the same token getter and no account selector. Iterate every page, then save the user's explicit account choice under your customer and client registration:

```typescript theme={null}
const discovery = new OFAuthClient({ accessToken: async () => currentAccessToken() })
for await (const account of discovery.linkedAccounts.iterate()) {
	console.log(account.id, account.display_name, account.status)
}
const selected = discovery.withLinkedAccount(selectedLinkedAccountId)
const profile = await selected.access.profile.get()
```

An account ID selects a resource. It does not grant permission or choose billing authority.

## Store customer tokens

For an app without an auth framework, `OFAuthLink` provides standard URL, PKCE, callback parsing, exchange, refresh, and revocation helpers:

```typescript theme={null}
import { OFAuthLink } from "@ofauth/onlyfans-sdk"

const link = new OFAuthLink({
	clientId: process.env.OFAUTH_CLIENT_ID!,
	clientSecret: process.env.OFAUTH_CLIENT_SECRET!,
	redirectUri: "https://your-app.example/oauth/ofauth/callback"
})
const attempt = await link.createAuthorizationUrl({ scopes: ["profile:read"] })
```

Persist `attempt.state` and `attempt.codeVerifier` with the current trusted application user, browser session, expiry, and registration version before redirecting to `attempt.url`.

On return, verify and atomically consume the matching pending record. A foreign session must not consume it. Then parse and exchange:

```typescript theme={null}
const { code } = link.parseCallback(request.url, pending.state)
const tokens = await link.exchangeCode({ code, codeVerifier: pending.codeVerifier })
```

Save tokens only if the registration version still matches. Calculate access expiry from `expires_in`. Refresh near expiry with `link.refresh({ refreshToken })`. The returned refresh token stays the same, so concurrent refresh and retry after a lost response require no distributed lock or attempt journal. Retain conditional writes when the user changes or disconnects a registration.

The [Express OAuth example](https://github.com/ofauth-source/example-express-oauth) shows PostgreSQL persistence, session-bound callbacks, account discovery and selection, targeted provider reconnect, and revocation. Its database stores ordinary application state, not an SDK-specific refresh protocol.

## Handle failures

API failures throw `OFAuthAPIError`. Read `code`, `recoveryAction`, and `requestId`. Follow [Handle failed requests](https://docs.ofauth.com/v3/reference/error-handling.md).

An invalid refresh grant requires authorization again. Transient refresh failures can be retried. A provider reconnect error needs [targeted reconnect](https://docs.ofauth.com/v3/guides/reconnect.md); OAuth refresh cannot repair it. Never repeat an interrupted message, post, or story automatically.


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