---
title: "DWS Processor API with Java"
canonical_url: "https://www.nutrient.io/guides/dws-processor/supported-languages/java/"
md_url: "https://www.nutrient.io/guides/dws-processor/supported-languages/java.md"
last_updated: "2026-05-18T14:28:53.296Z"
description: "Learn to use Nutrient DWS Processor API to process documents, make HTTP requests, and integrate API tools in your Java project."
---

# DWS Processor API with Java

With Nutrient DWS Processor API, you can process your documents by making HTTP requests to one of our [50+ API tools](https://dashboard.nutrient.io/processor-api/playground/). You can make a single request to one tool or combine API actions to generate, edit, OCR, and convert your document (1 document can have multiple API actions).

This guide explains how to use Java to make HTTP requests with our API by:

1. Installing the required dependencies

2. Preparing the payload

3. Making the request

You’ll use [OkHttp](https://square.github.io/okhttp/) to make HTTP requests, and you’ll use [`org.json`](https://mvnrepository.com/artifact/org.json/json) to build the `instructions` JSON object.

## Installing the required dependencies

You’ll need a Gradle-based Java project. The easiest way is to use [IntelliJ](https://www.jetbrains.com/idea/download/) and create one from there.![Screen capture of the IntelliJ new project wizard.](@/assets/guides/dws-processor/intellij-wizard.png)

Now you can add your dependencies to the `build.gradle` file:

```groovy

dependencies {
    implementation 'com.squareup.okhttp3:okhttp:4.9.2'
    implementation 'org.json:json:20210307'
}

```

Finally, you’ll need to add `document.pdf` and `logo.png` files to the root of your Java project. You can use the sample files provided by us — [document.pdf](https://www.nutrient.io/assets/guides/dws-processor/watermark/document.pdf) and [logo.png](https://www.nutrient.io/assets/guides/dws-processor/watermark/logo.png) — or use your own.

Your project is now ready to make requests to Nutrient DWS Processor API. Next, you’ll add your main entry point to the app. Create a new `PspdfkitApiExample` class and add a main method. You can also add all the imports you’re going to use:

```java

import okhttp3.*;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

public class PspdfkitApiExample {

    public static void main(String[] args) throws IOException {
      // Implement your call to Nutrient DWS Processor API here.
    }
}

```

Make sure you set this class as your app’s entry point. At this point, you’ll be able to run this class directly from IntelliJ.

Next, implement your main method. For this guide, you’ll watermark a PDF. First, you need a new `OkHttpClient`:

```java

final OkHttpClient client = new OkHttpClient().newBuilder().build();

```

Next, you’ll prepare the payload.

## Preparing the payload

For this example, you don’t need any special configuration to create your `instructions` JSON object:

```java

final JSONObject instructions = new JSONObject().put("parts", new JSONArray().put(new JSONObject().put("file", "document")
    )
  ).put("actions", new JSONArray().put(new JSONObject().put("type", "watermark").put("image", "company-logo").put("width", "50%")
    ).put(new JSONObject().put("type", "watermark").put("text", "Property of Nutrient").put("width", 150).put("height", 20).put("left", 0).put("bottom", "100%")
    )
  );

```

Here, you just build a JSON object using the [`org.json`](https://mvnrepository.com/artifact/org.json/json) dependency you added earlier. For examples of how the watermark API can be used (for example, adding multiple watermarks to the document, watermarking the last page of the document, and watermark alignments), refer to our [PDF watermark API](https://www.nutrient.io/api/pdf-watermark-api/) page.

Next, you can create your `RequestBody`:

```java

final RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart(
    "document",
    "document.pdf",
    RequestBody.create(
      new File("document.pdf"),
      MediaType.parse("application/pdf")
    )
  ).addFormDataPart(
    "company-logo",
    "logo.png",
    RequestBody.create(
      new File("logo.png"),
      MediaType.parse("image/png")
    )
  ).addFormDataPart("instructions", instructions.toString()).build();

```

Here, you assemble your `multipart/form-data` body containing the JSON instructions, your `document.pdf` that will be watermarked, and the `logo.png` that you’ll use as a watermark.

## Making the request

Finally, all that’s left is to actually make the request. Make sure to replace the `your_api_key_here` placeholder with your actual API key if it hasn’t yet been replaced:

```java

final Request request = new Request.Builder().url("https://api.nutrient.io/build").method("POST", body).addHeader("Authorization", "Bearer your_api_key_here").build();

final Response response = client.newCall(request).execute();

if (response.isSuccessful()) {
  Files.copy(
    response.body().byteStream(),
    FileSystems.getDefault().getPath("result.pdf"),
    StandardCopyOption.REPLACE_EXISTING
  );
} else {
  // Handle the error.
  throw new IOException(response.body().string());
}

```

This will actually send your request to Nutrient DWS Processor API and save the resulting PDF in the root folder as `result.pdf`.

At this point, you’ll be able to run this directly from IntelliJ and see `result.pdf` appear in your root folder. And that’s it — you now have everything set up to use Nutrient DWS Processor API from Java.

While this example made use of our watermarking API, this same approach can be used for all our available [tools](https://dashboard.nutrient.io/processor-api/playground/).

## Complete code

For your convenience, the complete code is below:

```java

import okhttp3.*;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

public class PspdfkitApiExample {

  public static void main(String[] args) throws IOException {
    final OkHttpClient client = new OkHttpClient().newBuilder().build();

    final JSONObject instructions = new JSONObject().put("parts", new JSONArray().put(new JSONObject().put("file", "document")
        )
      ).put("actions", new JSONArray().put(new JSONObject().put("type", "watermark").put("image", "company-logo").put("width", "50%")
        ).put(new JSONObject().put("type", "watermark").put("text", "Property of Nutrient").put("width", 150).put("height", 20).put("left", 0).put("bottom", "100%")
        )
      );

    final RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart(
        "document",
        "document.pdf",
        RequestBody.create(
          new File("document.pdf"),
          MediaType.parse("application/pdf")
        )
      ).addFormDataPart(
        "company-logo",
        "logo.png",
        RequestBody.create(
          new File("logo.png"),
          MediaType.parse("image/png")
        )
      ).addFormDataPart("instructions", instructions.toString()).build();

    final Request request = new Request.Builder().url("https://api.nutrient.io/build").method("POST", body).addHeader("Authorization", "Bearer your_api_key_here").build();

    final Response response = client.newCall(request).execute();

    if (response.isSuccessful()) {
      Files.copy(
        response.body().byteStream(),
        FileSystems.getDefault().getPath("result.pdf"),
        StandardCopyOption.REPLACE_EXISTING
      );
    } else {
      // Handle the error.
      throw new IOException(response.body().string());
    }
  }
}

```
---

## Related pages

- [Supported languages](/guides/dws-processor/supported-languages.md)
- [DWS Processor API with C#](/guides/dws-processor/supported-languages/csharp.md)
- [Enable enhanced code completion and documentation for Claude Code using the Nutrient DWS SDK.](/guides/dws-processor/supported-languages/javascript.md)
- [DWS Processor API with PHP](/guides/dws-processor/supported-languages/php.md)
- [DWS Processor API with other languages](/guides/dws-processor/supported-languages/other.md)
- [Enable enhanced code completion and documentation for Claude Code using the Nutrient DWS SDK.](/guides/dws-processor/supported-languages/python.md)

