# Turn on two-factor authentication

`POST https://api.twittrapi.com/client/2fa/enable`

Turn on authenticator-app (TOTP) two-factor authentication for your account and get the secret, a code that works right now and the backup code back.

HTML page: https://www.twittrapi.com/docs/account-actions/client-2fa-enable

> Account actions run with the session cookie of your own X account and go out through your own proxy. Use only accounts you control and follow X's rules; automated posting, following or liking can get the account limited or suspended. Treat the cookie like a password. Trial keys cannot call these endpoints.

Runs the same steps as Settings → Security → Two-factor authentication → Authentication app on x.com. The API links the authenticator for you: it reads the shared secret from X, works out the code and confirms it, so no phone or separate app is needed.

On success you get the secret (keep it to make codes later), an otpauth:// URI for a QR code, the code that is valid right now and a single-use backup code.

Some accounts need an e-mail step first. When they do, the first call answers with status "email_verification_required" and a flow_token, and X e-mails a code to the account. Read that code and call again with the same flow_token and email_code to finish.

## Parameters

| Name | In | Type | Required | Default | Description |
| --- | --- | --- | --- | --- | --- |
| `cookie` | body | string | yes |  | Cookie header of your own logged-in X session: auth_token=...; ct0=... (guest_id is used when present). How to copy it: /docs/x-session. |
| `proxy` | body | string | yes |  | Your proxy, format http://user:pass@host:port. The requests to X go out through it. Used only for this request. |
| `password` | body | string | yes |  | The account password, to start the flow. Leave out only when you resume with flow_token. |
| `flow_token` | body | string | no |  | Only to resume after an e-mail step: the flow_token from the "email_verification_required" answer. |
| `email_code` | body | string | no |  | Only together with flow_token: the code X e-mailed to the account. |

Body parameters are sent as a JSON object with `Content-Type: application/json`.

## Notes

- If the account already has authenticator 2FA on, the flow does not return a secret and the call ends with an error; turn it off first to enroll again.
- The password, the secret and the codes are not stored or logged by the API.
- No simple format: with format=simple the answer is the usual {status_code, response} wrapper.
- The `cookie` and `proxy` fields: https://www.twittrapi.com/docs/x-session

## Response

On success: {"status":"enabled","secret":"<base32 secret>","otpauth_uri":"otpauth://totp/...","totp":"<6-digit code valid now>","backup_code":"<single-use backup code>"}. When an e-mail step is needed first: {"status":"email_verification_required","flow_token":"<token>","message":"<which address X mailed>"} — call again with flow_token and email_code.

## Errors

| Status | When |
| --- | --- |
| 400 | The body is not valid JSON. |
| 422 | password is missing on a fresh call, flow_token is sent without email_code, cookie or proxy is missing, or the cookie has no ct0. Also: the account password or the e-mailed code was not accepted. |
| 407 | The proxy is unreachable or timed out. |
| 503 | X did not accept the request right now. Try again later; nothing was changed. |
| X's status | Any other status X answers with, passed through with X's message. |

Shared errors (401 missing key, 402 no balance, 429 rate limit, 5xx) are listed on https://www.twittrapi.com/docs/helpful-infos/error-handling. Calls that fail with 400, 407, 408, 409 or a 5xx error are refunded.

## Example request

curl:

```bash
curl -X POST 'https://api.twittrapi.com/client/2fa/enable' \
  --header 'X-API-KEY: YOUR_API_KEY' \
  --header 'Content-Type: application/json' \
  --data '{"cookie":"auth_token=...; ct0=...","proxy":"http://user:pass@host:port","password":"account-password"}'
```

Python:

```python
import httpx

resp = httpx.post(
    "https://api.twittrapi.com/client/2fa/enable",
    headers={"X-API-KEY": "YOUR_API_KEY"},
    json={
        "cookie": "auth_token=...; ct0=...",
        "proxy": "http://user:pass@host:port",
        "password": "account-password",
    },
    timeout=60,
)
print(resp.status_code, resp.json())
```

Node.js:

```js
const resp = await fetch("https://api.twittrapi.com/client/2fa/enable", {
  method: "POST",
  headers: { "X-API-KEY": process.env.TWITTRAPI_API_KEY, "Content-Type": "application/json" },
  body: JSON.stringify({
    cookie: "auth_token=...; ct0=...",
    proxy: "http://user:pass@host:port",
    password: "account-password",
  }),
});
console.log(resp.status, await resp.json());
```
