Client supports Bearer Token (app-only), OAuth 2.0 with PKCE (user context), and OAuth 1.0a (user context). Get your credentials from the X Developer Console.
Bearer Token
For public read, search, and streaming endpoints:import { Client, type ClientConfig, type Users } from "@xdevplatform/xdk";
const config: ClientConfig = { bearerToken: process.env.X_BEARER_TOKEN };
const client = new Client(config);
const userResponse: Users.GetByUsernameResponse =
await client.users.getByUsername("XDevelopers");
console.log(userResponse.data?.username);
import { Client } from "@xdevplatform/xdk";
const client = new Client({ bearerToken: process.env.X_BEARER_TOKEN });
const userResponse = await client.users.getByUsername("XDevelopers");
console.log(userResponse.data?.username);
OAuth 2.0 with PKCE
For user-authorized actions (creating posts, bookmarks, DMs, X Chat):import {
Client,
OAuth2,
generateCodeVerifier,
generateCodeChallenge,
type OAuth2Config,
type OAuth2Token,
} from "@xdevplatform/xdk";
const oauth2Config: OAuth2Config = {
clientId: process.env.X_CLIENT_ID!,
clientSecret: process.env.X_CLIENT_SECRET,
redirectUri: "http://localhost:8080/callback",
scope: ["tweet.read", "users.read", "offline.access"],
};
const oauth2 = new OAuth2(oauth2Config);
const codeVerifier = generateCodeVerifier();
const codeChallenge = await generateCodeChallenge(codeVerifier);
oauth2.setPkceParameters(codeVerifier, codeChallenge);
const authUrl = await oauth2.getAuthorizationUrl("example-state");
// Direct the user to authUrl and read the `code` query param from the callback
const tokens: OAuth2Token = await oauth2.exchangeCode(authCode, codeVerifier);
const client = new Client({ accessToken: tokens.access_token });
import {
Client,
OAuth2,
generateCodeVerifier,
generateCodeChallenge,
} from "@xdevplatform/xdk";
const oauth2 = new OAuth2({
clientId: process.env.X_CLIENT_ID,
clientSecret: process.env.X_CLIENT_SECRET,
redirectUri: "http://localhost:8080/callback",
scope: ["tweet.read", "users.read", "offline.access"],
});
const codeVerifier = generateCodeVerifier();
const codeChallenge = await generateCodeChallenge(codeVerifier);
oauth2.setPkceParameters(codeVerifier, codeChallenge);
const authUrl = await oauth2.getAuthorizationUrl("example-state");
const tokens = await oauth2.exchangeCode(authCode, codeVerifier);
const client = new Client({ accessToken: tokens.access_token });
const me = await client.users.getMe();
console.log(me.data);
OAuth 1.0a
OAuth 1.0a is disabled for newly created apps and is scheduled to be retired on January 31, 2027. Newly created apps must use OAuth 2.0 with PKCE or Bearer Token. Existing apps with OAuth 1.0a enabled can continue using it until January 31, 2027 — see Migrate from OAuth 1.0a to OAuth 2.0.
import { Client, OAuth1, type OAuth1Config, type Users } from "@xdevplatform/xdk";
const oauth1Config: OAuth1Config = {
apiKey: process.env.X_API_KEY!,
apiSecret: process.env.X_API_SECRET!,
accessToken: process.env.X_ACCESS_TOKEN!,
accessTokenSecret: process.env.X_ACCESS_TOKEN_SECRET!,
};
const client = new Client({ oauth1: new OAuth1(oauth1Config) });
const response: Users.GetMeResponse = await client.users.getMe();
console.log(response.data);
import { Client, OAuth1 } from "@xdevplatform/xdk";
const oauth1 = new OAuth1({
apiKey: process.env.X_API_KEY,
apiSecret: process.env.X_API_SECRET,
accessToken: process.env.X_ACCESS_TOKEN,
accessTokenSecret: process.env.X_ACCESS_TOKEN_SECRET,
});
const client = new Client({ oauth1 });
const response = await client.users.getMe();
console.log(response.data);