Implementation of Twitter internal API in TypeScript
npm install twitter-openapi-typescriptsh
npm i twitter-openapi-typescript
`
Usage
`typescript
import { TwitterOpenApi } from 'twitter-openapi-typescript';
const api = new TwitterOpenApi();
const client = await api.getGuestClient();
const response = await client.getUserApi().getUserByScreenName({ screenName: 'elonmusk' });
const userLegacy = response.data?.user?.legacy;
if (userLegacy) {
console.log(userLegacy.screenName);
console.log(followCount: ${userLegacy.friendsCount} followersCount: ${userLegacy.followersCount});
} else {
console.log('User not found');
}
`
$3
`typescript
// ct0 and authToken will also work but it is recommended to set all cookies.
const client = await api.getClientFromCookies({
ct0: '',
auth_token: '',
});
`
$3
The Token can only be used on the same OS that issued the Token
In other words, if the sec-ch-ua-platform does not match, the Token cannot be used.
This library uses the Linux Chrome header by default.
To use Token issued by Windows, do the following.
`typescript
const api = new TwitterOpenApi();
api.setAdditionalApiHeaders({
'sec-ch-ua-platform': '"Windows"',
});
`
$3
You should read the Test case.
$3
Most values exist as static variables. There is no need to change them.
Changing them could result in account suspension.
For advanced customization, use the
You can also use the TwitterOpenApiClient class directly
`typescript
import { TwitterOpenApi } from 'twitter-openapi-typescript';
TwitterOpenApi.fetchApi = fetch.bind(globalThis);
TwitterOpenApi.twitter = 'https://x.com/home';
TwitterOpenApi.bearer = 'xxxx';
``