twitterize
v2.2.2
Published
Send authorized requests to the Twitter API.
Downloads
40
Maintainers
Readme
Twitterize 🔑
Simple and minimalist wrapper using the native https
NodeJS module to send authorized requests to the Twitter API.
No dependencies and super small: .
Install
npm install twitterize
Use
Create an app and get your credentials, you will need:
- API KEY
- API SECRET KEY
- ACCESS TOKEN
- ACCESS TOKEN SECRET
Use the documented endpoints and parameters for the twitter API.
For example, to search tweets:
/* eslint-disable no-console */
const twitterize = require('../src')
const twit = twitterize({
api_key: '<YOUR API KEY>',
api_secret_key: '<YOUR API SECRET KEY>',
access_token: '<YOUR ACCESS TOKEN>',
access_token_secret: '<YOUR SECRET ACCESS TOKEN>',
})
const options = {
requestMethod: 'GET',
endpoint: '/search/tweets.json',
queryParams: { q: 'twitter bot' },
}
twit(options).then(console.log).catch(console.log)
To post tweets:
const twitterize = require('../src')
const twit = twitterize({
api_key: '<YOUR API KEY>',
api_secret_key: '<YOUR API SECRET KEY>',
access_token: '<YOUR ACCESS TOKEN>',
access_token_secret: '<YOUR SECRET ACCESS TOKEN>',
})
const options = {
requestMethod: 'POST',
endpoint: '/statuses/update.json',
bodyParams: { status: 'Hello World!' },
}
twit(options).then(console.log).catch(console.log)
To upload an image:
const fs = require('fs')
const path = require('path')
const twitterize = require('../src')
const twit = twitterize({
api_key: '<YOUR API KEY>',
api_secret_key: '<YOUR API SECRET KEY>',
access_token: '<YOUR ACCESS TOKEN>',
access_token_secret: '<YOUR SECRET ACCESS TOKEN>',
})
const imagePath = path.join(__dirname, './cat.jpg')
const b64content = fs.readFileSync(imagePath, { encoding: 'base64' })
// Image upload
twit({
requestMethod: 'POST',
subdomain: 'upload',
endpoint: '/media/upload.json',
bodyParams: { media_data: b64content },
})
.then((data) =>
// Status update
twit({
requestMethod: 'POST',
endpoint: '/statuses/update.json',
bodyParams: {
status: 'Hello World IND SIG!',
media_ids: JSON.parse(data).media_id_string,
},
}),
)
.then(console.log)
.catch(console.log)
Examples
To run the examples clone the repository:
git clone https://github.com/MauricioRobayo/twitterize.git
Install dependencies:
cd twitterize
npm install
In the root directory of the repo create a .env
file with your credentials (you can get them from your twitter app page):
# .env file
TWITTER_API_KEY="<YOUR API KEY>"
TWITTER_API_SECRET_KEY="<YOUR API SECRET KEY>"
TWITTER_ACCESS_TOKEN="<YOUR ACCESS TOKEN>"
TWITTER_ACCESS_TOKEN_SECRET="<YOUR SECRET ACCESS TOKEN>"
Use the provided npm scripts:
npm run example:search
npm run example:post
npm run example:upload
TypeScript
Type definitions are included. A TypeScript example is provided here, you can run it with npm run example:search-ts
.