{"id":14168,"library":"twitter-api-sdk","title":"Twitter API TypeScript SDK","description":"This is an official TypeScript SDK designed for interacting with the Twitter API, specifically supporting only Twitter API v2. It provides comprehensive type information for both requests and responses, streamlining development for TypeScript and JavaScript users. The SDK supports OAuth2 (App-only Bearer Token and OAuth 2.0 User Context with PKCE) and is compatible with Node.js 14+ environments. A significant limitation is its explicit lack of support for browser environments due to the Twitter API's absence of CORS support. As of its latest version, 1.2.1, the SDK is clearly marked as 'beta' and 'not ready for production,' indicating ongoing development and potential instability. Its release cadence is not formally defined, but as a TwitterDev project, it is expected to evolve with the Twitter API itself.","status":"maintenance","version":"1.2.1","language":"javascript","source_language":"en","source_url":"https://github.com/twitterdev/twitter-api-typescript-sdk","tags":["javascript","typescript"],"install":[{"cmd":"npm install twitter-api-sdk","lang":"bash","label":"npm"},{"cmd":"yarn add twitter-api-sdk","lang":"bash","label":"yarn"},{"cmd":"pnpm add twitter-api-sdk","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The SDK is built with TypeScript and primarily intended for ESM (ES Modules) usage in Node.js environments. CommonJS require statements are not idiomatic.","wrong":"const Client = require('twitter-api-sdk');","symbol":"Client","correct":"import { Client } from 'twitter-api-sdk';"},{"note":"The `auth` object is a named export from the main package and contains authentication-related classes like `OAuth2User`.","wrong":"import { OAuth2User } from 'twitter-api-sdk';","symbol":"auth","correct":"import { Client, auth } from 'twitter-api-sdk';"},{"note":"The `OAuth2User` class is accessed as a property of the named `auth` export, not typically as a direct named import or from a subpath.","wrong":"import { OAuth2User } from 'twitter-api-sdk/auth';","symbol":"auth.OAuth2User","correct":"import { auth } from 'twitter-api-sdk'; const authClient = new auth.OAuth2User({ ... });"}],"quickstart":{"code":"import { Client } from 'twitter-api-sdk';\n\nconst bearerToken = process.env.BEARER_TOKEN ?? ''; // Ensure BEARER_TOKEN is set in your environment\n\nasync function getTweet() {\n  if (!bearerToken) {\n    console.error('BEARER_TOKEN environment variable is not set.');\n    process.exit(1);\n  }\n\n  const client = new Client(bearerToken);\n  try {\n    const tweet = await client.tweets.findTweetById(\"20\"); // Example tweet ID\n    if (tweet.data) {\n      console.log('Fetched Tweet Text:', tweet.data.text);\n    } else {\n      console.log('No tweet data found for ID 20.');\n      if (tweet.errors) {\n        console.error('API Errors:', tweet.errors);\n      }\n    }\n  } catch (error) {\n    console.error('Error fetching tweet:', error);\n  }\n}\n\ngetTweet();","lang":"typescript","description":"Initializes the Twitter API client with a bearer token and fetches a single tweet by its ID, demonstrating basic API interaction and error handling."},"warnings":[{"fix":"Monitor GitHub repository for updates and carefully review changelogs before deploying to production. Consider alternative, more stable libraries for critical production systems if beta status is a concern.","message":"This SDK is currently in beta and is explicitly stated as 'not ready for production use.' Developers should be aware of potential instabilities or breaking changes in future minor versions.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Refer to the Twitter API v2 documentation to ensure all intended API calls are compatible with v2 specifications. Migrate any legacy v1.1 logic to v2 patterns.","message":"The SDK exclusively supports Twitter API v2. Attempting to use endpoints or features specific to older API versions (e.g., v1.1) will not work and may result in errors.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For browser-based applications, all Twitter API calls must be proxied through a server-side component (e.g., a Node.js backend using this SDK). Do not attempt direct API calls from the browser.","message":"This SDK is strictly designed for Node.js environments and does not function in web browsers. The Twitter API itself does not support CORS, leading to 'Cross-Origin Request Blocked' errors in browser contexts.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always use environment variables or a secure configuration management system to store and access API keys and secrets. Ensure environment variables are correctly loaded in your Node.js application.","message":"The documentation mentions `process.env.CLIENT_ID` and `process.env.CLIENT_SECRET` for OAuth2 setup. These credentials should never be hardcoded or exposed in client-side code, even in Node.js applications, for security reasons.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"search_vec":"'1.2.1':91 '14':62 '2.0':52 'absenc':82 'api':2,17,22,80,130 'app':46 'app-on':45 'bearer':48 'beta':98 'browser':74 'cadenc':112 'clear':95 'compat':59 'comprehens':26 'context':54 'cor':84 'defin':116 'design':11 'develop':35,106 'due':76 'environ':63,75 'evolv':126 'expect':124 'explicit':69 'formal':115 'indic':104 'inform':28 'instabl':109 'interact':13 'javascript':39,132 'lack':70 'latest':89 'limit':66 'mark':96 'node.js':61 'oauth':51 'oauth2':44 'offici':8 'ongo':105 'pkce':56 'potenti':108 'product':103 'project':121 'provid':25 'readi':101 'releas':111 'request':31 'respons':33 'sdk':4,10,42,93 'signific':65 'specif':18 'streamlin':34 'support':19,43,72,85 'token':49 'twitter':1,16,21,79,129 'twitterdev':120 'type':27 'typescript':3,9,37,133 'user':40,53 'v2':23 'version':90","created_at":"2026-04-20T01:58:16.401347+00:00","updated_at":"2026-04-20T01:58:16.401347+00:00","problems":[{"fix":"The Twitter API does not support Cross-Origin Resource Sharing (CORS). This SDK is intended for server-side (Node.js) use only. Implement a backend proxy to relay requests to the Twitter API from your browser application.","cause":"Attempting to make Twitter API requests directly from a web browser environment.","error":"Access to fetch at 'https://api.twitter.com/2/...' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource."},{"fix":"Ensure you import the `auth` object as a named export and access `OAuth2User` as its property: `import { auth } from 'twitter-api-sdk'; const authClient = new auth.OAuth2User({ ... });`","cause":"Incorrectly importing or accessing the `OAuth2User` class, typically by trying to destructure it directly from the main package or using a wrong import path.","error":"TypeError: Cannot read properties of undefined (reading 'OAuth2User') at Object.auth"},{"fix":"Double-check your API keys, bearer token, and OAuth 2.0 client credentials. Ensure they are correct, active, and have the required scopes (e.g., `tweet.read`, `users.read`) configured in your Twitter Developer App.","cause":"An invalid, expired, or missing Bearer Token or OAuth 2.0 credentials were provided, or the application lacks necessary permissions/scopes for the requested endpoint.","error":"Error: Request failed with status code 401: Unauthorized"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":"https://developer.twitter.com","github":"https://github.com/twitterdev/twitter-api-typescript-sdk","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/twitter-api-sdk","openapi_spec":null,"status_page":null,"smithery":null,"categories":["communication","http-networking","auth-security"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-18","install_tag":null}}