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

# Go SDK

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

Use Go 1.23 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

Use the [HTTP quickstart](https://docs.ofauth.com/v3/setup/quickstart.md) until the SDK release candidate is published.

In your Go module, run:

```bash theme={null}
go get github.com/ofauth-org/onlyfans-sdk-go/v4@v4.0.0-rc.1
```

## Read the current profile

Set `OFAUTH_ACCESS_TOKEN` and `OFAUTH_LINKED_ACCOUNT_ID` in your server's environment. Save this as `main.go` and run `go run main.go`:

```go theme={null}
package main

import (
	"context"
	"encoding/json"
	"log"
	"os"

	ofauth "github.com/ofauth-org/onlyfans-sdk-go/v4"
)

func main() {
	token := os.Getenv("OFAUTH_ACCESS_TOKEN")
	accountID := os.Getenv("OFAUTH_LINKED_ACCOUNT_ID")
	if token == "" || accountID == "" {
		log.Fatal("Set both OFAuth environment variables")
	}

	client := ofauth.NewClient(ofauth.Config{
		AccessToken: ofauth.StaticToken(token),
		LinkedAccountID: accountID,
	})
	profile, err := client.Access.Profile.Get(
		context.Background(), ofauth.AccessProfileGetParams{},
	)
	if err != nil {
		log.Fatal(err)
	}
	if err := json.NewEncoder(os.Stdout).Encode(profile); err != nil {
		log.Fatal(err)
	}
}
```

The SDK sends the access token and linked account header for you.

## Check the result

The program prints the account's profile as JSON. Check its `id`, `username`, and `name`. Missing values stay `null`. This request does not change the account.

If the API returns an error, use `errors.As` to read it as `*ofauth.APIError`. Its `Code`, `RecoveryAction`, and `RequestID` help you choose the next step. See [Handle failed requests](https://docs.ofauth.com/v3/reference/error-handling.md). 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 `main`, list the accounts shared through this authorization:

```go theme={null}
for account, err := range client.LinkedAccounts.Iterate(
	context.Background(), ofauth.LinkedAccountsListParams{},
) {
	if err != nil {
		log.Fatal(err)
	}
	log.Println(account.ID)
}
```

Use `client.WithLinkedAccount(accountID)` to select another account shared through the same authorization. Pass a context with a deadline or cancellation when your app needs to stop a request.

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