> ## Documentation Index
> Fetch the complete documentation index at: https://buttercms.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Java SDK

> The ButterCMS Java SDK provides type-safe, strongly-typed response objects for Spring Boot, Quarkus, and vanilla Java applications.

## Installation

> [tab] Maven
>
> Add to your `pom.xml`:
>
> ```xml theme={null}
> <dependency>
>   <groupId>com.buttercms</groupId>
>   <artifactId>buttercmsclient</artifactId>
>   <version>1.12.0</version>
> </dependency>
> ```

> [tab] Gradle
>
> Add to your `build.gradle`:
>
> ```gradle theme={null}
> dependencies {
>     implementation 'com.buttercms:buttercmsclient:1.12.0'
> }
> ```

## Initialization

Initialize the SDK with your Read API token from the [ButterCMS Settings](https://buttercms.com/settings/).

```java theme={null}
import com.buttercms.IButterCMSClient;
import com.buttercms.ButterCMSClient;

// Basic initialization (preview mode disabled)
IButterCMSClient client = new ButterCMSClient("your_api_token", false);

// With preview mode enabled
boolean isPreviewEnabled = true;
IButterCMSClient client = new ButterCMSClient("your_api_token", isPreviewEnabled);
```

## Configuration options

The client constructor accepts an optional custom `HttpClient` as a third argument:

```java theme={null}
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClients;

// With custom HttpClient (preview mode is the second argument, HttpClient the third)
HttpClient customClient = HttpClients.custom().build();

IButterCMSClient client = new ButterCMSClient(
    "your_api_token",
    false,        // preview mode
    customClient  // custom Apache HttpClient
);
```

## API methods

All methods return strongly-typed response objects.

### Pages

Retrieve and search pages created in the ButterCMS dashboard.

```java theme={null}
import com.buttercms.model.Page;
import com.buttercms.model.PageResponse;
import com.buttercms.model.PagesResponse;
import java.util.HashMap;
import java.util.Map;

// Build query parameters
Map<String, String> params = new HashMap<>();
params.put("locale", "en");
params.put("preview", "1");
params.put("levels", "2");

// Fetch a single page (pass a Class<T> to deserialize the fields into)
PageResponse<Map> pageResponse = client.getPage("landing_page", "home-page", params, Map.class);
Page<Map> page = pageResponse.getData();
System.out.println(page.getFields().get("headline"));

// Fetch paginated pages
params.put("page", "1");
params.put("page_size", "10");
PagesResponse<Map> pages = client.getPages("landing_page", params, Map.class);

for (Page<Map> p : pages.getData()) {
    System.out.println(p.getSlug());
}

// Access pagination metadata
System.out.println("Next page: " + pages.getMeta().getNextPage());
```

### Collections

Fetch content from Collections (structured data tables).

```java theme={null}
import com.buttercms.model.CollectionResponse;
import java.util.Map;

Map<String, String> params = new HashMap<>();
params.put("locale", "en");

// Fetch collection content (one collection slug per call, pass a Class<T> for the items)
CollectionResponse<Map> response = client.getCollection("faq", params, Map.class);

// Access collection data
for (Map item : response.getData().getItems()) {
    System.out.println(item.get("question"));
    System.out.println(item.get("answer"));
}
```

### Blog Posts

Access the built-in Blog Engine for posts, categories, tags, and authors.

```java theme={null}
import com.buttercms.model.Post;
import com.buttercms.model.PostsResponse;
import com.buttercms.model.PostResponse;

// Build query parameters
Map<String, String> params = new HashMap<>();
params.put("page", "1");
params.put("page_size", "10");
params.put("exclude_body", "true");

// List blog posts
PostsResponse postsResponse = client.getPosts(params);

for (Post post : postsResponse.getData()) {
    System.out.println(post.getTitle());
    System.out.println(post.getSlug());
}

// Access pagination
System.out.println("Total: " + postsResponse.getMeta().getCount());
System.out.println("Next page: " + postsResponse.getMeta().getNextPage());

// Fetch a single post
PostResponse postResponse = client.getPost("my-post-slug");
Post post = postResponse.getData();
System.out.println(post.getTitle());
System.out.println(post.getBody());

// Search posts
params.put("query", "search term");
PostsResponse searchResults = client.getSearchPosts(params);
```

### Authors

```java theme={null}
import com.buttercms.model.Author;
import com.buttercms.model.AuthorsResponse;
import com.buttercms.model.AuthorResponse;

Map<String, String> params = new HashMap<>();
params.put("include", "recent_posts");

// List all authors
AuthorsResponse authorsResponse = client.getAuthors(params);

for (Author author : authorsResponse.getData()) {
    System.out.println(author.getFirstName() + " " + author.getLastName());
}

// Fetch a single author
AuthorResponse authorResponse = client.getAuthor("jennifer-smith", params);
Author author = authorResponse.getData();
System.out.println(author.getBio());
```

### Categories

```java theme={null}
import com.buttercms.model.Category;
import com.buttercms.model.CategoriesResponse;
import com.buttercms.model.CategoryResponse;

Map<String, String> params = new HashMap<>();
params.put("include", "recent_posts");

// List all categories
CategoriesResponse categoriesResponse = client.getCategories(params);

for (Category category : categoriesResponse.getData()) {
    System.out.println(category.getName());
    System.out.println(category.getSlug());
}

// Fetch a single category
CategoryResponse categoryResponse = client.getCategory("news", params);
```

### Tags

```java theme={null}
import com.buttercms.model.Tag;
import com.buttercms.model.TagsResponse;
import com.buttercms.model.TagResponse;

Map<String, String> params = new HashMap<>();
params.put("include", "recent_posts");

// List all tags
TagsResponse tagsResponse = client.getTags(params);

for (Tag tag : tagsResponse.getData()) {
    System.out.println(tag.getName());
}

// Fetch a single tag
TagResponse tagResponse = client.getTag("featured", params);
```

## Query parameters reference

| Parameter | Applies To | Description |
| - | - | - |
| `page` | Posts, Pages | Page number for pagination |
| `page_size` | Posts, Pages | Number of items per page |
| `exclude_body` | Posts | Exclude post body for faster response |
| `author_slug` | Posts | Filter posts by author |
| `category_slug` | Posts | Filter posts by category |
| `tag_slug` | Posts | Filter posts by tag |
| `include` | Authors, Categories, Tags | Include `recent_posts` with response |
| `locale` | Pages, Collections | Locale code for localized content |
| `preview` | Pages | Set to `1` to include draft content |
| `levels` | Pages | Depth of nested references (1-5) |

## Complete example

```java theme={null}
import com.buttercms.ButterCMSClient;
import com.buttercms.IButterCMSClient;
import com.buttercms.model.*;
import java.util.*;

public class ButterCMSExample {

    public static void main(String[] args) {
        String apiToken = System.getenv("BUTTERCMS_API_TOKEN");
        IButterCMSClient client = new ButterCMSClient(apiToken, false);

        try {
            // Fetch the home page
            Map<String, String> pageParams = new HashMap<>();
            pageParams.put("locale", "en");
            PageResponse<Map> homePageResponse = client.getPage("landing_page", "home", pageParams, Map.class);
            Page<Map> homePage = homePageResponse.getData();
            System.out.println("Page headline: " + homePage.getFields().get("headline"));

            // Fetch navigation collection
            CollectionResponse<Map> collections = client.getCollection(
                "main_navigation",
                new HashMap<>(),
                Map.class
            );
            List<Map> navItems = collections.getData().getItems();
            System.out.println("Navigation items: " + navItems.size());

            // Fetch recent blog posts
            Map<String, String> postParams = new HashMap<>();
            postParams.put("page", "1");
            postParams.put("page_size", "3");
            postParams.put("exclude_body", "true");

            PostsResponse postsResponse = client.getPosts(postParams);
            System.out.println("Recent posts: " + postsResponse.getData().size());

            for (Post post : postsResponse.getData()) {
                System.out.println("  - " + post.getTitle());
            }

        } catch (Exception e) {
            System.err.println("Error fetching content: " + e.getMessage());
        }
    }
}
```

## Resources

> [card] [GitHub Repository](https://github.com/ButterCMS/buttercms-java)
>
> View source code, report issues, and contribute

> [card] [Maven Central](https://mvnrepository.com/artifact/com.buttercms/buttercmsclient)
>
> Package details and version history


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.