Skip to main content
Chappie handles authentication by guiding users through a device-code sign-in flow tied to their existing ChatGPT account. Once sign-in completes, the SDK stores credentials securely in the iOS or macOS Keychain — your app never receives raw tokens. Instead, you observe a published ChappieAuthState value and an optional ChappieAccountInfo struct that contains the user’s email, plan, and account identifiers. Chappie also refreshes expired tokens automatically before every request, so you don’t need to manage token lifetimes yourself.

How authentication works

1

User taps sign in

Your app calls startDeviceCodeSignIn() — or uses the ChappieSignIn button that calls it for you. The SDK requests a short-lived device code from OpenAI.
2

User approves in a browser

The user opens the verification URL (automatically or manually), signs in with their ChatGPT account, and pastes the device code. Chappie polls in the background until the code is approved.
3

Credentials stored in Keychain

When polling succeeds, the SDK writes the access and refresh tokens to Keychain using the accessibility and account settings you configured. Your app never sees the raw tokens.
4

Auth state published to your UI

ChappieAuthSession.state transitions to .signedIn and accountInfo is populated. Subsequent app launches restore the session from Keychain automatically.
5

Automatic token refresh

Before every request, Chappie checks whether the stored access token is expired. If it is, the SDK silently refreshes it using the stored refresh token and writes the new credential back to Keychain.

Creating an auth session

Create a single ChappieAuthSession as a @StateObject in the view that owns your sign-in flow. Chappie reads the Keychain at init time, so the session is already populated if the user signed in during a previous launch.
Pass a ChappieConfiguration to customise which Keychain slot is used, or to support multiple accounts. See Multi-Account for details.

Observing auth state

ChappieAuthSession is an ObservableObject with four published properties: React to state in your SwiftUI views to switch between signed-out and signed-in UI:

ChappieAuthState values


Account info

After a successful sign-in, authSession.accountInfo is a ChappieAccountInfo value with the following fields:

Signing out

Call signOut() to clear the stored Keychain credential and reset state to .signedOut. Any in-progress sign-in polling task is cancelled automatically.

Manual token refresh

Chappie refreshes credentials automatically before requests. If you need to force a refresh — for example, after the user upgrades their plan — call refreshAuth():
If the refresh token is invalid or missing, refreshAuth() transitions the session to .reauthenticationRequired and throws a ChappieAuthError. Prompt the user to sign in again in that case.

Error reference

When requiresReauthentication is true on a ChappieAuthError (currently .missingRefreshToken and certain token-exchange failures), the SDK automatically signs out and transitions to .reauthenticationRequired. All other errors leave existing credentials intact so a retry is possible.

Your app never receives raw access or refresh tokens through Chappie’s public API. ChappieAccountInfo exposes only identity and plan metadata derived from the stored credential.