Quickstart: Publish a knowledge base in QnA Maker using Java

This REST-based quickstart walks you through programmatically publishing your knowledge base (KB). Publishing pushes the latest version of the knowledge base to a dedicated Azure Search index and creates an endpoint that can be called in your application or chat bot.

This quickstart calls QnA Maker APIs:

  • Publish - this API doesn't require any information in the body of the request.


  • JDK SE (Java Development Kit, Standard Edition)

  • This sample uses the Apache HTTP client from HTTP Components. You need to add the following Apache HTTP client libraries to your project:

    • httpclient-4.5.3.jar
    • httpcore-4.4.6.jar
    • commons-logging-1.2.jar
  • Visual Studio Code

  • You must have a QnA Maker service. To retrieve your key, select Keys under Resource Management in your Azure dashboard for your QnA Maker resource. .

  • QnA Maker knowledge base (KB) ID found in the URL in the kbid query string parameter as shown below.

    QnA Maker knowledge base ID

    If you don't have a knowledge base yet, you can create a sample one to use for this quickstart: Create a new knowledge base.


The complete solution file(s) are available from the Azure-Samples/cognitive-services-qnamaker-java GitHub repository.

Create a Java file

Open VSCode and create a new file named PublishKB.java.

Add the required dependencies

At the top of PublishKB.java, above the class, add the following lines to add necessary dependencies to the project:

// httpclient-4.5.3.jar
// httpcore-4.4.6.jar
// commons-logging-1.2.jar

import java.net.URI;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.http.entity.StringEntity;

Create PublishKB class with main method

After the dependencies, add the following class:

public class PublishKB {

    public static void main(String[] args) 

Add required constants

In the main method, add the required constants to access QnA Maker. Replace the values with your own.

String knowledge_base_id = "YOUR-KNOWLEDGE-BASE-ID";
String resource_key = "YOUR-RESOURCE-KEY";

String host = "https://westus.api.cognitive.microsoft.com/qnamaker/v4.0/knowledgebases/" + knowledge_base_id;

Add POST request to publish knowledge base

After the required constants, add the following code, which makes an HTTPS request to the QnA Maker API to publish a knowledge base and receives the response:

// Create http client
HttpClient httpclient = HttpClients.createDefault();

HttpPost request = new HttpPost(host);

// set authorization

// Send request to Azure service, get response
HttpResponse response = httpclient.execute(request);

// No returned content, 204 == success

The API call returns a 204 status for a successful publish without any content in the body of the response. The code adds content for 204 responses.

For any other response, that response is returned unaltered.

Build and run the program

Build and run the program from the command line. It will automatically send the request to the QnA Maker API, then it will print to the console window.

  1. Build the file:

    javac -cp "lib/*" PublishKB.java
  2. Run the file:

    java -cp ".;lib/*" PublishKB

Clean up resources

When you are done with the quickstart, remove all the files created in this quickstart. Sign on to QnA Maker, and delete the KB.

Next steps

After the knowledge base is published, you need the endpoint URL to generate an answer.