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

> ## Agent Instructions
> Turnkey is wallet infrastructure: create and manage crypto wallets, sign transactions, and enforce policy-based access controls. Best-fit uses: embedded consumer wallets (email/passkey/social auth, no seed phrases), automated onchain operations with server-side wallets, AI agent wallets with policy-scoped signing, enterprise key management, and verifiable off-chain workloads on Turnkey Verifiable Cloud (TVC).
> Every API call is a JSON POST to https://api.turnkey.com signed with a P-256 API key; create an organization and key self-serve at https://app.turnkey.com.
> Key Turnkey developer resources: API reference (https://docs.turnkey.com/api-reference/overview/intro.md), OpenAPI spec (https://docs.turnkey.com/public_api.swagger.json), authentication (https://docs.turnkey.com/features/authentication/overview.md), webhooks (https://docs.turnkey.com/features/webhooks/overview.md), MCP server for docs search (https://docs.turnkey.com/mcp), skill for building with Turnkey (https://docs.turnkey.com/skill.md), agent skills for operating an organization (https://docs.turnkey.com/get-started/ai-skills.md), CLI (https://docs.turnkey.com/sdks/cli.md), SDK reference (https://docs.turnkey.com/sdks/introduction.md), full docs content (https://docs.turnkey.com/llms-full.txt).

# OAuth 2.0 authentication V2

> Exchange client-encrypted OAuth 2.0 authentication inputs for a Turnkey OIDC token.

`POST https://api.turnkey.com/public/v1/submit/oauth2_authenticate`

This URL is shared with the original `ACTIVITY_TYPE_OAUTH2_AUTHENTICATE` activity. Select V2 with `type: ACTIVITY_TYPE_OAUTH2_AUTHENTICATE_V2` and its encrypted payload. Use normal signed [API authentication](https://docs.turnkey.com/api-reference/overview/stamps.md).

## Request body

| Field | Required | Value |
| - | - | - |
| `type` | Yes | `ACTIVITY_TYPE_OAUTH2_AUTHENTICATE_V2` |
| `timestampMs` | Yes | Current Unix timestamp in milliseconds, as a string |
| `organizationId` | Yes | Same parent organization UUID used for initialization |
| `parameters.oauth2CredentialId` | Yes | Same credential UUID used for initialization and inside the encrypted payload |
| `parameters.encryptedMessage` | Yes | JSON string containing the serialized encrypted client message returned by the helper |
| `parameters.targetPublicKey` | Yes | Hex target public key returned by the helper for the verified target |
| `generateAppProofs` | No | Boolean requesting App Proofs |

First [initialize a target](https://docs.turnkey.com/api-reference/activities/initialize-oauth-20-authentication.md), verify its signature, and encrypt the [authentication inputs](#encrypted-payload) on the client. Send only the fields above; the authentication inputs travel inside `encryptedMessage`.

A target completes one exchange and expires after five minutes. Initialize a new target for each login attempt.

## Encrypted payload

Build this JSON object on the client, then encrypt it to the verified enclave target with `encryptOauth2ToBundle` from `@turnkey/crypto`. The helper verifies the target's TLS Fetcher signature, checks that the target was signed for `organization_id`, and returns the `encryptedMessage` string and the matching `targetPublicKey`.

```ts theme={"system"}
import { encryptOauth2ToBundle } from "@turnkey/crypto";

const { encryptedMessage, targetPublicKey } = await encryptOauth2ToBundle({
  bundle: {
    organizationId,
    oauth2CredentialId,
    clientId,
    provider: "OAUTH2_PROVIDER_DISCORD",
    authCode,
    codeVerifier,
    redirectUri,
    nonce,
  },
  enclaveTargetMessage, // from init_oauth2_authenticate
});
```

The helper takes camelCase inputs and serializes them to the snake\_case object below:

```json theme={"system"}
{
  "organization_id": "<parent organization UUID>",
  "oauth2_credential_id": "<OAuth credential UUID>",
  "client_id": "<provider client ID>",
  "provider": "OAUTH2_PROVIDER_DISCORD",
  "auth_code": "<authorization code from this provider redirect>",
  "code_verifier": "<PKCE verifier for this authorization attempt>",
  "redirect_uri": "<redirect URI used in the provider flow>",
  "nonce": "<lowercase hex SHA-256 digest of the client session public key>"
}
```

* Fields inside the encrypted object are **snake\_case**; the outer request uses **camelCase**.
* Set `provider` to `OAUTH2_PROVIDER_X` or `OAUTH2_PROVIDER_DISCORD`.
* Take `organization_id`, `client_id`, and `provider` from your client configuration.
* `code_verifier` must be 43 to 128 characters from `A-Z`, `a-z`, `0-9`, `-`, `.`, `_`, and `~`, as required by PKCE.
* `redirect_uri` must be an absolute URI without a fragment.
* Compute `nonce` as `sha256(publicKey)` over the client session public key, as described in [nonce restrictions](https://docs.turnkey.com/features/authentication/social-logins.md#nonce-restrictions-in-oidc-tokens). It must be 64 lowercase hex characters. Use the same session key for OAuth login.
* To receive the provider bearer token, add optional `bearer_token_target_public_key`: an uncompressed P-256 public key. Turnkey returns the bearer token encrypted to that key in the OIDC token's `encrypted_bearer_token` claim. Omit the field when unused rather than sending an empty string.

### Purpose binding

`TRANSPORT_ENCRYPTION_SUITE_ENCLAVE_ENCRYPT_V2` adds a purpose binding to the HPKE associated data. The binding contains the purpose `ENCRYPTED_INGRESS_PURPOSE_OAUTH2_AUTHENTICATE` and the `oauth2_credential_id`. The enclave rebuilds the binding from the outer `parameters.oauth2CredentialId`. If that value differs from the credential ID the client encrypted under, decryption fails. This stops a ciphertext from being replayed against a different credential or a different encrypted-ingress activity.

If you encrypt without the helper, verify the target's TLS Fetcher signature against Turnkey's production signing key and apply the same purpose binding. Don't log the unencrypted object.

## Result

A completed activity contains `activity.result.oauth2AuthenticateResult.oidcToken`. This field is not renamed to `oauth2AuthenticateResultV2`. SDKs may unwrap the result.

Use the token for existing account lookup, signup, or [OAuth login](https://docs.turnkey.com/api-reference/activities/login-with-oauth.md) with the same client session key you used to compute the nonce. It is not the final session JWT.

If the provider rejects the exchange, the activity fails with a generic authentication error that doesn't include provider details.

See [social logins](https://docs.turnkey.com/features/authentication/social-logins.md#authentication-sequence-diagrams) for the complete flow.


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