### Install Dependencies Source: https://github.com/xdevplatform/twitter-api-typescript-sdk/blob/main/examples/README.md Installs the necessary Node.js packages for the examples. This command should be run from the 'examples' directory of the SDK. ```shell npm install ``` -------------------------------- ### Run All Examples Source: https://github.com/xdevplatform/twitter-api-typescript-sdk/blob/main/examples/README.md Executes all TypeScript example files in the current directory using ts-node. This is a convenient way to test multiple functionalities. ```shell npx ts-node *.ts ``` -------------------------------- ### Run Specific Example (OAuth2 Bearer Token) Source: https://github.com/xdevplatform/twitter-api-typescript-sdk/blob/main/examples/README.md Executes a specific example file, 'oauth2-bearer.ts', using ts-node. This example likely demonstrates authentication using a bearer token. ```shell npx ts-node oauth2-bearer.ts ``` -------------------------------- ### Environment Variables Configuration Source: https://github.com/xdevplatform/twitter-api-typescript-sdk/blob/main/examples/README.md Sets up the required environment variables in a .env file. These variables are used for authentication and API access. Not all variables are needed for every example. ```env BEARER_TOKEN=my-bearer-token CLIENT_ID=my-client-id CLIENT_SECRET=my-client-secret ``` -------------------------------- ### Install Twitter API SDK Source: https://github.com/xdevplatform/twitter-api-typescript-sdk/blob/main/README.md This snippet shows the command to install the Twitter API SDK for TypeScript using npm. It is the first step to integrate the SDK into your project. ```shell npm install twitter-api-sdk ``` -------------------------------- ### Iterate Over Tweet Stream Source: https://github.com/xdevplatform/twitter-api-typescript-sdk/blob/main/README.md A simplified example of iterating over a tweet stream using an async generator. This pattern is useful for real-time data consumption from the Twitter API. ```typescript const stream = client.tweets.sampleStream(); for await (const tweet of stream) { console.log(tweet.data.text); } ``` -------------------------------- ### Consume Sample Tweet Stream Source: https://github.com/xdevplatform/twitter-api-typescript-sdk/blob/main/README.md This example shows how to consume a sample stream of tweets from the Twitter API. It uses an async generator to iterate over incoming tweets and logs the author ID of each tweet. It requires a bearer token for authentication. ```typescript import { Client } from "twitter-api-sdk"; const client = new Client(process.env.BEARER_TOKEN); async function main() { const stream = client.tweets.sampleStream({ "tweet.fields": ["author_id"], }); for await (const tweet of stream) { console.log(tweet.data?.author_id); } } main(); ``` -------------------------------- ### Build and Test SDK Source: https://github.com/xdevplatform/twitter-api-typescript-sdk/blob/main/README.md These commands are used for building the TypeScript SDK and running its test suite. They are essential for developers contributing to the project or verifying its integrity. ```shell yarn build yarn test ``` -------------------------------- ### Clone Twitter API SDK Repository Source: https://github.com/xdevplatform/twitter-api-typescript-sdk/blob/main/README.md This command shows how to clone the official TypeScript SDK repository from GitHub. This is typically done by developers who want to contribute to the SDK or inspect its source code. ```shell git clone https://github.com/twitterdev/twitter-api-typescript-sdk ``` -------------------------------- ### Create Confidential OAuth2 User Client Source: https://github.com/xdevplatform/twitter-api-typescript-sdk/blob/main/README.md Demonstrates setting up an OAuth2 client for a confidential application. This requires client ID, client secret, callback URL, and specified scopes for secure user authentication. ```typescript const authClient = new auth.OAuth2User({ client_id: process.env.CLIENT_ID, client_secret: process.env.CLIENT_SECRET, callback: "http://127.0.0.1:3000/callback", scopes: ["tweet.read", "users.read", "offline.access"], }); const client = new Client(authClient); ``` -------------------------------- ### Create Public OAuth2 User Client Source: https://github.com/xdevplatform/twitter-api-typescript-sdk/blob/main/README.md Shows how to create an OAuth2 client for a public application. This involves providing client ID, callback URL, and requested scopes for user authentication flows. ```typescript const authClient = new auth.OAuth2User({ client_id: process.env.CLIENT_ID, callback: "http://127.0.0.1:3000/callback", scopes: ["tweet.read", "users.read", "offline.access"], }); const client = new Client(authClient); ``` -------------------------------- ### Handle Paginated Results (User Followers) Source: https://github.com/xdevplatform/twitter-api-typescript-sdk/blob/main/README.md Illustrates how to handle paginated API responses, specifically for fetching user followers. It shows iterating through pages using an async generator and also accessing the first page directly. ```typescript const followers = client.users.usersIdFollowers("20"); for await (const page of followers) { console.log(page.data); } // This also works const followers = await client.users.usersIdFollowers("20"); console.log(followers.data); ``` -------------------------------- ### Initialize Client with Bearer Token Source: https://github.com/xdevplatform/twitter-api-typescript-sdk/blob/main/README.md Demonstrates how to initialize the Twitter API client using a bearer token. This is a common authentication method for accessing the API, especially for read-only operations. ```typescript import { Client } from "twitter-api-sdk"; const client = new Client("MY-BEARER-TOKEN"); ``` -------------------------------- ### Generate SDK from OpenAPI Spec Source: https://github.com/xdevplatform/twitter-api-typescript-sdk/blob/main/README.md Illustrates how to use the `yarn generate` script to regenerate the SDK code from an OpenAPI specification. This can be done using the latest online spec or a local file, specifying a SemVer version. ```shell yarn generate 1.0.0 yarn generate 1.0.0 --specFile ~/path/to/file/openapi.json ``` -------------------------------- ### Request OAuth2 Access Token Source: https://github.com/xdevplatform/twitter-api-typescript-sdk/blob/main/README.md This snippet shows the process of requesting an access token after a user has completed the OAuth authorization flow and returned a code. This token is then used to make authenticated API requests on behalf of the user. ```typescript await authClient.requestAccessToken(code); ``` -------------------------------- ### Generate OAuth2 Authentication URL Source: https://github.com/xdevplatform/twitter-api-typescript-sdk/blob/main/README.md Provides the code to generate the authorization URL for the OAuth2 flow. This URL is used to redirect users to Twitter to grant permissions to your application. ```typescript const authUrl = authClient.generateAuthURL({ code_challenge_method: "s256", }); ``` -------------------------------- ### Fetch a Tweet by ID Source: https://github.com/xdevplatform/twitter-api-typescript-sdk/blob/main/README.md This snippet demonstrates how to fetch a specific tweet by its ID using the SDK. It makes an asynchronous call to the API and logs the text content of the retrieved tweet. A bearer token is required for authentication. ```typescript import { Client } from "twitter-api-sdk"; const client = new Client(process.env.BEARER_TOKEN); async function main() { const tweet = await client.tweets.findTweetById("20"); console.log(tweet.data.text); } main(); ``` -------------------------------- ### Revoke OAuth2 Access Token Source: https://github.com/xdevplatform/twitter-api-typescript-sdk/blob/main/README.md Demonstrates how to revoke an access token previously granted to the application. This is a security measure to end a user's session or remove application permissions. ```typescript const response = await authClient.revokeAccessToken(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.