Hey there, fellow dev! Ready to dive into the world of Tumblr API integration? Buckle up, because we're about to embark on a journey that'll have you posting, fetching, and manipulating Tumblr data like a pro. We'll be using the nifty tumblr.js package, so let's get started!
Before we jump in, make sure you've got:
First things first, let's get our project off the ground:
mkdir tumblr-api-project cd tumblr-api-project npm init -y npm install tumblr.js
Easy peasy, right? Now we're ready to rock and roll!
Time to get cozy with Tumblr's API. Head over to the Tumblr API console and create a new application. You'll get some shiny new API keys – treat them like your secret sauce!
Now, let's set up our tumblr.js client:
const tumblr = require('tumblr.js'); const client = tumblr.createClient({ consumer_key: 'YOUR_CONSUMER_KEY', consumer_secret: 'YOUR_CONSUMER_SECRET', token: 'YOUR_TOKEN', token_secret: 'YOUR_TOKEN_SECRET' });
Let's flex those API muscles with some basic operations:
client.blogInfo('your-blog-name.tumblr.com', (err, data) => { if (err) console.error(err); console.log(data); });
client.blogPosts('your-blog-name.tumblr.com', (err, data) => { if (err) console.error(err); console.log(data.posts); });
client.createTextPost('your-blog-name.tumblr.com', { title: 'Hello World', body: 'This is my first post via the Tumblr API!' }, (err, data) => { if (err) console.error(err); console.log('Post created!', data); });
Ready to level up? Let's tackle some advanced stuff:
function getAllPosts(blogName, offset = 0, allPosts = []) { client.blogPosts(blogName, { offset }, (err, data) => { if (err) console.error(err); allPosts = allPosts.concat(data.posts); if (data.posts.length === 20) { getAllPosts(blogName, offset + 20, allPosts); } else { console.log('All posts:', allPosts); } }); }
tumblr.js makes it a breeze to work with various post types. Here's a quick example with photo posts:
client.createPhotoPost('your-blog-name.tumblr.com', { caption: 'Check out this awesome pic!', source: 'https://example.com/image.jpg' }, (err, data) => { if (err) console.error(err); console.log('Photo posted!', data); });
Always be prepared! Here's how to handle those pesky errors and rate limits:
function makeApiCall(apiFunction, ...args) { return new Promise((resolve, reject) => { apiFunction(...args, (err, data) => { if (err) { if (err.status === 429) { console.log('Rate limited. Retrying in 60 seconds...'); setTimeout(() => makeApiCall(apiFunction, ...args), 60000); } else { reject(err); } } else { resolve(data); } }); }); }
When things go sideways (and they will), the Tumblr API console is your best friend. Use it to test your API calls and make sure you're sending the right data.
Pro tip: Liberal use of console.log()
never hurt anyone during development!
When you're ready to ship your awesome integration, remember:
And there you have it! You're now armed and dangerous with Tumblr API knowledge. Remember, the key to mastering any API is practice and experimentation. So go forth and create something awesome!
Need more info? The tumblr.js documentation and Tumblr API docs are treasure troves of information.
Happy coding, and may your blogs always be reblogged!