An Introduction to the Danbooru Public API

Danbooru is a popular image board that hosts a wide variety of anime images and related content. The service also provides a rich set of APIs that allow users to programmatically access its data and content. In this article, we'll introduce you to the Danbooru Public API, and provide you with some examples of how to use it using JavaScript.

Requirements

To access Danbooru's API, you need to obtain an API key. To do this, you'll need a Danbooru account. You can sign up for an account here: https://danbooru.donmai.us/signup

Once you have an account, you'll need to find your API key. You can find your API key in your account settings page.

Endpoint

The main endpoint for Danbooru's API is https://danbooru.donmai.us.

Examples

Search for Images

To search for images using the API, you can use the /posts.json endpoint. Here is an example using JavaScript:

const API_URL = 'https://danbooru.donmai.us/posts.json';

async function searchImages(apiKey, tags) {
  const params = new URLSearchParams({
    tags,
    api_key: apiKey,
    limit: 10 // Set limit to 10 results
  });

  const response = await fetch(`${API_URL}?${params}`);
  const data = await response.json();

  return data.map((post) => ({
    title: post.tags,
    url: post.large_file_url
  }));
}

// Example usage
searchImages('your-api-key', 'kaguya-sama').then((results) => {
  console.log(results);
});

This example searches for images with the tag "kaguya-sama" and returns an array of objects containing the image's title and a URL to the large-sized file.

Create a New Tag

To create a new tag using the API, you can use the /tags.json endpoint. Here is an example using JavaScript:

const API_URL = 'https://danbooru.donmai.us/tags.json';

async function createTag(apiKey, name, category, aliases = []) {
  const body = new URLSearchParams({
    name,
    category,
    aliases: aliases.join(','),
    api_key: apiKey,
  });

  const response = await fetch(API_URL, {
    method: 'POST',
    body,
  });

  const data = await response.json();
  return data;
}

// Example usage
createTag('your-api-key', 'my-new-tag', 'artist').then((tag) => {
  console.log(`Created tag "${tag.name}" with ID ${tag.id}`);
});

This example creates a new tag with the name "my-new-tag" and the category "artist". You can optionally specify aliases for the tag by providing an array of strings as the aliases parameter.

Conclusion

The Danbooru Public API provides a powerful way to access the site's data and content programmatically. We hope this introduction and these examples help you get started building your own applications and tools using Danbooru's API. If you have any questions or feedback, feel free to reach out to the Danbooru community.

Related APIs