In Java, we'll use the package introduced in Java 11 to send HTTP requests. Here's how you can use the book search API with Java:

Firstly, you need to define a Book class to model the data you will be receiving:

public class Book {
    private String title;
    private String author;
    // Getters and setters
    public String getTitle() {
        return title;
    public void setTitle(String title) {
        this.title = title;
    public String getAuthor() {
        return author;
    public void setAuthor(String author) { = author;

Then, you can use the following code to make the API request:

import java.util.Map;

public class Main {
    public static void main(String[] args) throws Exception {
        HttpClient client = HttpClient.newHttpClient();
        String apiKey = System.getenv("PROMPTJOY_API_KEY");
        Map<String, String> values = Map.of("query", "your_search_term");
        String body = new ObjectMapper().writeValueAsString(values);
        HttpRequest request = HttpRequest.newBuilder()
            .header("Content-Type", "application/json")
            .header("x-api-key", apiKey)
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
        Book[] books = new ObjectMapper().readValue(response.body(), Book[].class);
        for (Book book : books) {
            System.out.println("Title: " + book.getTitle() + ", Author: " + book.getAuthor());

Replace "your_search_term" with the book you're searching for. This function will return an array of books.

Remember to handle exceptions and errors as needed in your actual application code.

Note: This example uses the ObjectMapper class from the Jackson library to convert JSON to and from Java objects. Ensure that you've added Jackson to your project's dependencies. Also, the API key is retrieved from environment variables for security reasons. Ensure that you've set the PROMPTJOY_API_KEY environment variable in your environment where this code will be executed.

Last updated