Turn on two-factor authentication
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.
/client/2fa/enableYour account, your proxy, your risk.
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; that is between you and X. Treat the cookie like a password: it gives full access to the account. Trial keys cannot call these endpoints.
How to copy the cookie and which proxy to use: Your X session (cookie).
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
cookiebody · string · requiredCookie header of your own logged-in X session: auth_token=...; ct0=... (guest_id is used when present). How to copy it: /docs/x-session.
proxybody · string · requiredYour proxy, format http://user:pass@host:port. The requests to X go out through it. Used only for this request.
passwordbody · string · requiredThe account password, to start the flow. Leave out only when you resume with flow_token.
flow_tokenbody · string · optionalOnly to resume after an e-mail step: the flow_token from the "email_verification_required" answer.
email_codebody · string · optionalOnly 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
cookieandproxyfields: Your X session (cookie).
Example request
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"}'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())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());Base URL https://api.twittrapi.com, key in the X-API-KEY header. The SDKs do not wrap account actions, so the examples are plain HTTP calls.
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.
In the raw format, field names and nesting come from X and can change without notice. See Response format.
Errors
- 400The body is not valid JSON.
- 422password 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.
- 407The proxy is unreachable or timed out.
- 503X did not accept the request right now. Try again later; nothing was changed.
- X'sAny other status X answers with, passed through with X's message.
Errors every endpoint can return (401, 402, 403, 404, 408, 409, 429, 5xx), and which of them are charged, are on the Errors page. Calls that fail with 400, 407, 408, 409 or a 5xx error are refunded.