Generate fake tweets images as a Now microservice.
Supports Twemoji and tweet quotes.
An express server renders the tweet using an EJS template. A puppeteer instance is fired and screenshots the page. The image is then returned by the endpoint.
Append the URI queries to https://tweet-generator.now.sh/tweet to generate a tweet webpage.
Append the URI queries to https://tweet-generator.now.sh/screenshot to generate a tweet image .
Name | Description |
---|---|
style |
Tweet template to use (see Tweet templates) |
tweetData |
Stringified JSON object containing tweet's data (see Tweet data object) |
Name | Description |
---|---|
classic |
Normal tweet (default if not specified) |
no-stats |
Normal tweet without statistics |
A JSON stringified object containing the tweet's properties.
If you have UTF-16 (like recent emojis) content, JSON stringify, URI encode and convert to Base64. See Implementation example.
Name | type | Required | Default |
---|---|---|---|
pseudo | string |
โ | |
handle | string |
โ | |
content | string |
โ | |
verified | boolean |
false |
|
date | Date |
Current date | |
retweets | number |
0 |
|
likes | number |
0 |
|
replies | number |
0 |
|
avatar | string |
Default Twitter image | |
quoted | Object - all of the above except quoted |
No quote |
You can use the provided demo endpoint or host your own using Now. Examples are given at the root of the website.
https://tweet-generator.now.sh/
The following ESM module script (.mjs
) will generate a tweet image and download it to generatedTweet.png
.
import fs from 'fs'
import fetch from 'node-fetch'
const setup = async () => {
// My future tweet data
const tweet = {
pseudo: 'My cool pseudo ๐',
handle: 'my_handle',
content: 'My awesome tweet content ๐',
verified: true,
date: new Date(),
retweets: 54371,
likes: 54371,
replies: 543,
avatar: 'https://cdn.pixabay.com/photo/2016/03/09/16/47/woman-1246844_960_720.jpg',
quoted: {
pseudo: 'Quoted pseudo ๐คทโโ๏ธ',
handle: 'quoted_handle',
content: 'quoted tweet content',
verified: true,
date: new Date(Date.now() - 3694200),
retweets: 1,
likes: 14,
replies: 21,
avatar: 'https://cdn.pixabay.com/photo/2016/03/09/16/46/hiking-1246836__340.jpg'
}
}
// Create the uri (encodeURIComponent is important as stringified JSON can contain invalid query characters)
const uri = `https://tweet-generator.now.sh/screenshot?style=classic&tweetData=${encodeURIComponent(JSON.stringify(tweet))}`)
/*
// If you have UTF-16 (like recent emojis), JSON stringify, URI encode and convert to Base64
const btoa = require('btoa')
const uri = `https://tweet-generator.now.sh/screenshot?style=classic&tweetData=${btoa(encodeURIComponent(JSON.stringify(tweet)))}`
*/
const { body } = await fetch(uri)
.then(async res => {
// The endpoint returned errors, throw
if (!res.ok) throw (await res.json()).errors.join(', ')
return res
})
// Save the response body to an image file
return new Promise((resolve, reject) => {
const fileStream = fs.createWriteStream('./generatedTweet.png')
body.pipe(fileStream)
body.on('error', err => reject(err))
fileStream.on('finish', () => resolve())
})
}
setup()
If you want to contribute to this project, you can open an issue detailing your suggestions or bugs.
Feel free to open a pull request.
Author of this service is not affiliated in any way with Twitter, Inc
.