Use Contentstack to power content for your Java projects

Create Java-based applications and use the Java SDK to fetch and deliver content from Contentstack. The Java SDK uses the Content Delivery APIs.

Getting Started

This guide will help you get started with our Java SDK to build apps powered by Contentstack.


To get started with Java SDK, you will need an IDE. You can use an IDE of your choice, but make sure it supports Java.

SDK Installation and Setup

You can add the following dependency code snippets into your project:

  • Maven

    Maven users need to add the above code in your pom.xml file under the <dependencies> section.

  • Gradle
    compile 'com.contentstack.sdk:java:1.4.0'

    Gradle users need to add the above dependency code into your build.gradle file.

Initialize SDK

Contentstack offers two regions (US and European) as data centers to store customers' account details and data. Both regions are independent of each other and therefore has a dedicated set of instructions to use SDKs offered by Contentstack.

To use SDKs for the European region, you will have to make certain changes in the configuration of the SDK, as detailed below, and the rest of the instructions remain the same.

To initialize the SDK, you will need to specify the API Key, Delivery Token, and Environment Name of your stack.

Stack stack = Contentstack.stack("stack_API_key", "delivery_token", "environment_name");

For Setting the European Region:

Config config = Config();
Config.region = ContentstackRegion.EU;
Stack stack = Contentstack.stack("API_key", "delivery_token", "environment", config);

Once you have initialized the SDK, you can query entries to fetch the required content in your app.

Basic Queries

Get a Single Entry

To retrieve a single entry from a content type, use the code snippet given below:

ContentType contentType = stack.contentType("content_type_uid");
Entry blogEntry = contentType.entry("entry_uid");
blogEntry.fetch(new EntryResultCallBack() {
    public void onCompletion(ResponseType responseType, Error error) {
        if (error == null) {
            // Success block
        } else {
            // Error block 

Get Multiple Entries

To retrieve multiple entries of a particular content type, use the code snippet given below:

//stack is an instance of Stack class
Query blogQuery = stack.contentType("content_type_uid").query();
blogQuery.find(new QueryResultsCallBack() {
    public void onCompletion(ResponseType responseType, QueryResult queryResult, Error error) {
        if (error == null) {
            //Success block
        } else {
            //Error block

These were examples of some of the basic queries of the SDK. For advanced queries, refer to our API reference documentation by visiting the link given below.

API Reference

Go through our Java API Reference guide to know about the methods that can be used to query your content in Contentstack.

Read Java API Reference Guide


To help you get started, we have provided Java code snippet below. This snippet shows how to make queries and fetch entries from Contentstack. 

public class News {

    public static void main(String[] args) {
        Stack stack = null;
        try {
            stack = Contentstack.stack("blt920bb7e90248f607", "blt0c4300391e033d4a59eb2857", "production");
        } catch (Exception ex) {
        Query query = stack.contentType("news").query();
        query.find(new QueryResultsCallBack() {
            public void onCompletion(ResponseType responseType, QueryResult queryresult, Error error) {
                if (error == null) {
                    for (Entry entry: queryresult.getResultObjects()) {
                        System.out.println("Title: " + entry.getString("title"));
                } else {
                    System.out.println("Failed: " + error.getErrorCode());


Title: Uber aims to raise $2.1 billion to fund India, China expansion
Title: The Martian Movie Review Ticket Received
Title: Dhoni to play charitable golf event in US
Title: Indian economy offers hope as China struggles
Was this article helpful?