Registry / communication / twitter-api-sdk

twitter-api-sdk

JSON →
library1.2.1jsnpmunverified

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.

npm install twitter-api-sdk
INSTALL
IMPORT
SIG · TWITTER-API-SDK
T
twitter-api-sdk
communicationjavascriptv1.2.1
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Client
✓ import { Client } from 'twitter-api-sdk';
✗ const Client = require('twitter-api-sdk');
The SDK is built with TypeScript and primarily intended for ESM (ES Modules) usage in Node.js environments. CommonJS require statements are not idiomatic.
auth
✓ import { Client, auth } from 'twitter-api-sdk';
✗ import { OAuth2User } from 'twitter-api-sdk';
The `auth` object is a named export from the main package and contains authentication-related classes like `OAuth2User`.
auth.OAuth2User
✓ import { auth } from 'twitter-api-sdk'; const authClient = new auth.OAuth2User({ ... });
✗ import { OAuth2User } from 'twitter-api-sdk/auth';
The `OAuth2User` class is accessed as a property of the named `auth` export, not typically as a direct named import or from a subpath.

Initializes the Twitter API client with a bearer token and fetches a single tweet by its ID, demonstrating basic API interaction and error handling.

import { Client } from 'twitter-api-sdk'; const bearerToken = process.env.BEARER_TOKEN ?? ''; // Ensure BEARER_TOKEN is set in your environment async function getTweet() { if (!bearerToken) { console.error('BEARER_TOKEN environment variable is not set.'); process.exit(1); } const client = new Client(bearerToken); try { const tweet = await client.tweets.findTweetById("20"); // Example tweet ID if (tweet.data) { console.log('Fetched Tweet Text:', tweet.data.text); } else { console.log('No tweet data found for ID 20.'); if (tweet.errors) { console.error('API Errors:', tweet.errors); } } } catch (error) { console.error('Error fetching tweet:', error); } } getTweet();
Debug
Known issues
gotchaThis 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.
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.
affects: >=1.0.0
gotchaThe 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.
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.
affects: >=1.0.0
gotchaThis 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.
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.
affects: >=1.0.0
gotchaThe 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.
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.
affects: >=1.0.0
Errors
Common errors & fixes
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.
Attempting to make Twitter API requests directly from a web browser environment.
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.
TypeError: Cannot read properties of undefined (reading 'OAuth2User') at Object.auth
Incorrectly importing or accessing the `OAuth2User` class, typically by trying to destructure it directly from the main package or using a wrong import path.
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({ ... });`
Error: Request failed with status code 401: Unauthorized
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.
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.
Upgrade
Version history
1.2.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
41 hits · last 30 days
node
34
OpenAI (training)
1
Resources