danbooru-node
danbooru api wrapper
This api wrapper allows you to access Danbooru's API with functions and promises.
It works in Node.js and bundled for browsers where fetch is available.
const Danbooru = // Perform a search for popular image postsconst booru = booru
Jump to section: Using this module • Upgrading from previous versions
Using this module
Once I've fleshed out this module a bit more, I plan to create more complete documentation for it. For now, here are some things you can do.
Danbooru class
This module exports a Danbooru
class. Calling its constructor with no argument allows you to make unauthenticated requests to https://danbooru.donmai.us/.
const booru =
If you would like to make authenticated requests, you can pass an authentication string in the format login:api_key
.
const login = 'login'const key = 'api_key'const booru = login + ':' + key
If you have an alternate Danbooru address you would like to connect to, you also specify that in this string.
const booru = 'http://safebooru.donmai.us/' const login = 'login'const key = 'api_key' const authenticatedBooru = `https://:@sonohara.donmai.us`
Getting an array of posts
Posts represent images on Danbooru. You can query them like this:
const posts = await booru
Your parameters are passed directly to Danbooru's API:
limit
- The number of posts you'd like to retrieve on one page.page
- The page number you'd like to retrieve. Bigger numbers have older posts.tags
- Your Danbooru search tags. You can use two tags unauthenticated, as well asrating:safe
as a third tag for free.md5
- An image md5 to search for.random
- A boolean that randomizes your results.raw
- Disables tag parsing, treatingtags
as a single literal tag.
You'll get an array of objects that looks like this: https://danbooru.donmai.us/posts.json
Getting a single post by its id
You can also look up a single post using the same function:
const post = await booru
You'll get an object: https://danbooru.donmai.us/posts/2560676.json
Getting an image
Once you have a post, you'll likely want to extract its image. The most useful object properties for this purpose are:
file_url
: The post image. For small images, this is the same aslarge_file_url
.large_file_url
: The image you see by default on Danbooru, which may be scaled down.preview_file_url
: The tiny thumbnail used to represent this image.
You can pass one of these values to booru.url()
to generate a URL
object, which you can turn into an absolute url by accessing url.href
or just typecasting it.
const post = await booruconst url = booru // Node.jsconst request = http // Browsersconst response = await
Manipulating favorites
Previous versions of this library have had functions to manipulate favorites for an authenticated user, so you can still do that with this version.
// Add a favoritebooru // Remove a favoritebooru
Please note that due to Danbooru's security settings, this function may fail in browsers.
Other endpoints
If you would like to do something I haven't added to this module yet, you can use these methods:
boorubooruboorubooru
They all take two arguments. The first is a path, and the second is your parameters. They'll be sent as JSON for POST
, PUT
, and DELETE
requests, and as part of a query string for GET
requests.
Your paths' leading slashes are optional, but don't add extensions or query strings. These functions will automatically add .json
and any specified query string properties to the end.
Due to Danbooru's security settings, custom GET
requests should work in browsers, but other methods may fail.
Upgrading from previous versions
This module was completely rewritten for each major release before this one.
Version 1 used callbacks, so upgrading involves completely rewriting your code.
Version 2 used promises like the current version does, so it should be possible to upgrade your code by swapping out some function calls, though you will need to rewrite code involving the old Post
type.
Instantiation
This module's class constructor now always takes a string.
The Safebooru subclass has been removed. You can still specify https://safebooru.donmai.us
manually.
// Version 2const booru = 'login' 'api_key' // Version 3const booru = 'login:api_key'
// Version 2const booru = login: 'login' api_key: 'api_key' base: 'https://safebooru.donmai.us' // Version 2 and 3const booru = 'https://login:api_key@safebooru.donmai.us'
Searching for posts
Searching for posts now always takes a parameter object.
// Version 2const posts = await booru // Version 2 and 3const posts = await booru
Fetching posts
Getting individual posts is now performed via the main .posts()
function
// Version 2const post = await booruposts // Version 3const post = await booru
Posts object
There is no longer a posts object. These functions all return normal JavaScript objects that you can interact with normally.
Please refer to Getting an image above for details on how to download images. Many of the properties on the old .file
object can still be accessed as part of the post data object.
// Version 2postraw // Version 3post
// Version 2posttags // Version 3posttag_string
// Version 2Stringpostrating // Version 3postrating
Favorites
Functions that work with favorites have been renamed.
// Version 2 or 3const favorites = await booru
// Version 2boorufavoritesboorufavorites // Version 3boorubooru
// Version 2boorufavoritesboorufavorites // Version 3boorubooru
Using previous versions
If you prefer older versions of this module, you can still install them with one of these commands, and find documentation for them on GitHub.
- danbooru-node v2.0.3:
npm install danbooru@2
- danbooru-node v1.4.8:
npm install danbooru@1