> This is Payabli documentation. For a complete page index, fetch https://docs.payabli.com/llms.txt — append .md to any page URL for lightweight markdown. For section-level indexes, query parameters, and other AI-optimized access methods, see https://docs.payabli.com/ai-agents.md

# Use the Java SDK

> Learn how to install and use the Java SDK to develop apps

Payabli offers an official Software Development Kit (SDK) for the Java programming language.
The official Java SDK can be installed in your projects to support app development and provide type safety when calling Payabli's APIs.
Most development environments can use the SDK to generate code suggestions and inline documentation.
See the [sdk-java](https://github.com/payabli/sdk-java) package for more information.

## Dependencies

Before you begin, make sure you have the following installed on your machine:

* [JDK](https://www.oracle.com/java/technologies/javase-downloads.html) 11+
* [Maven](https://maven.apache.org/install.html) 3.6+
* [git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)

## Use the SDK

This section shows you how to install and use the Payabli SDK in a new Java project.
The example code shows how to use the SDK to make a transaction with the `moneyIn().getpaidv2` method.
The `moneyIn().getpaidv2` method calls the [POST /v2/MoneyIn/getpaid](/developers/api-reference/moneyinV2/make-a-transaction) endpoint.
See the [SDK reference](https://github.com/payabli/sdk-java/blob/main/reference.md) for a full list of methods.

Method names in the SDK correspond to endpoint names in the API reference.
For example: the `notification().addNotification` method calls the [POST Notification](/developers/api-reference/notification/add-notification) endpoint.

Open your terminal and run the following commands to create a new directory:

```bash
mkdir my-payabli-app
cd my-payabli-app
```

Create a basic `pom.xml` file with the Payabli SDK dependency:

```xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
         http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>my-payabli-app</artifactId>
    <version>1.0.0</version>
    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>io.github.payabli</groupId>
            <artifactId>sdk-java</artifactId>
            <version>[0.0.310,)</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>4.12.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <mainClass>example.PayabliExample</mainClass>
                    <classpathScope>runtime</classpathScope>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
```

Create the Maven directory structure:

```bash
mkdir -p src/main/java/com/example
```

Create a new file called `PayabliExample.java` in `src/main/java/com/example/`:

```bash
touch src/main/java/com/example/PayabliExample.java
```

Open the `PayabliExample.java` file in your code editor.

/// Import the SDK classes

Import the Payabli SDK to make it available in your code.
/// Initialize the client

Create an authenticated client instance with your API key.
This client has methods that call Payabli's API endpoints.
The `environment` method specifies which environment to use.
Use `Environment.SANDBOX` for testing and `Environment.PRODUCTION` for production.
/// Build the payment request

Construct a request object that contains all the necessary fields to process a transaction. Include payment details, a payment method, and customer data.
/// Execute the transaction

Call `moneyIn().getpaidv2()` to process the transaction. The client calls Payabli's `POST /v2/MoneyIn/getpaid` endpoint.
/// Show the result

Print key fields from the response to confirm the transaction succeeded.

```java
// PayabliExample.java
package example;

import io.github.payabli.api.PayabliApiClient;
import io.github.payabli.api.PayabliApiClientBuilder;
import io.github.payabli.api.resources.moneyin.requests.RequestPaymentV2;
import io.github.payabli.api.types.V2TransactionResponseWrapper;
import io.github.payabli.api.types.TransRequestBody;
import io.github.payabli.api.types.PaymentDetail;
import io.github.payabli.api.types.PaymentMethod;
import io.github.payabli.api.types.PayMethodCredit;
import io.github.payabli.api.types.PayMethodCreditMethod;
import io.github.payabli.api.types.PayorDataRequest;
import io.github.payabli.api.core.Environment;

public class PayabliExample {
    public static void main(String[] args) {
        PayabliApiClient client = new PayabliApiClientBuilder()
                .apiKey("REPLACE_WITH_YOUR_API_KEY")
                .environment(Environment.SANDBOX)
                .build();

        PaymentDetail paymentDetails = PaymentDetail.builder()
                .totalAmount(100.0)
                .serviceFee(0.0)
                .build();

        PayorDataRequest customerData = PayorDataRequest.builder()
                .customerId(4440L)
                .build();

        PayMethodCredit paymentMethod = PayMethodCredit.builder()
                .cardexp("02/27")
                .cardnumber("4111111111111111")
                .method(PayMethodCreditMethod.CARD)
                .cardcvv("999")
                .cardHolder("Kassiane Cassian")
                .cardzip("12345")
                .initiator("payor")
                .build();

        TransRequestBody requestBody = TransRequestBody.builder()
                .paymentDetails(paymentDetails)
                .paymentMethod(PaymentMethod.of(paymentMethod))
                .customerData(customerData)
                .entryPoint("REPLACE_WITH_YOUR_ENTRYPOINT")
                .ipaddress("255.255.255.255")
                .build();

        RequestPaymentV2 request = RequestPaymentV2.builder()
                .body(requestBody)
                .build();

        V2TransactionResponseWrapper result = client.moneyIn().getpaidv2(request);

        System.out.println("Reason: " + result.getReason());
        System.out.println("Transaction ID: " + result.getData().getPaymentTransId());
        System.out.println("Auth code: " + result.getData().getResponseData().getAuthcode().orElse(null));
    }
}
```

Compile and run the app with Maven:

```bash
mvn clean compile exec:java
```

Check the console output for the result of the transaction.
A successful transaction returns output like this:

```txt
    Reason: Approved
    Transaction ID: 255-9242c1dd69ac44f292c44be7a995e8b1
    Auth code: TAS815
```

Open your terminal and run the following commands to create a new directory:

```bash
mkdir my-payabli-app
cd my-payabli-app
```

Create a `build.gradle` file with the Payabli SDK dependency:

```groovy
plugins {
    id 'java'
    id 'application'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'io.github.payabli:sdk-java:[0.0.310,)'
    implementation 'com.squareup.okhttp3:okhttp:4.12.0'
}

application {
    mainClass = 'example.PayabliExample'
}
```

Create the Gradle directory structure:

```bash
mkdir -p src/main/java/example
```

Create a new file called `PayabliExample.java` in `src/main/java/example/`:

```bash
touch src/main/java/example/PayabliExample.java
```

Open the `PayabliExample.java` file in your code editor.

/// Import the SDK classes

Import the Payabli SDK to make it available in your code.

```java
// PayabliExample.java
package example;

import io.github.payabli.api.PayabliApiClient;
import io.github.payabli.api.PayabliApiClientBuilder;
import io.github.payabli.api.resources.moneyin.requests.RequestPaymentV2;
import io.github.payabli.api.types.V2TransactionResponseWrapper;
import io.github.payabli.api.types.TransRequestBody;
import io.github.payabli.api.types.PaymentDetail;
import io.github.payabli.api.types.PaymentMethod;
import io.github.payabli.api.types.PayMethodCredit;
import io.github.payabli.api.types.PayMethodCreditMethod;
import io.github.payabli.api.types.PayorDataRequest;
import io.github.payabli.api.core.Environment;
```

/// Initialize the client

Create an authenticated client instance with your API key.
This client has methods that call Payabli's API endpoints.
The `environment` method specifies which environment to use.
Use `Environment.SANDBOX` for testing and `Environment.PRODUCTION` for production.

```java focus=16-23
// PayabliExample.java
package example;

import io.github.payabli.api.PayabliApiClient;
import io.github.payabli.api.PayabliApiClientBuilder;
import io.github.payabli.api.resources.moneyin.requests.RequestPaymentV2;
import io.github.payabli.api.types.V2TransactionResponseWrapper;
import io.github.payabli.api.types.TransRequestBody;
import io.github.payabli.api.types.PaymentDetail;
import io.github.payabli.api.types.PaymentMethod;
import io.github.payabli.api.types.PayMethodCredit;
import io.github.payabli.api.types.PayMethodCreditMethod;
import io.github.payabli.api.types.PayorDataRequest;
import io.github.payabli.api.core.Environment;

public class PayabliExample {
    public static void main(String[] args) {
        PayabliApiClient client = new PayabliApiClientBuilder()
                .apiKey("REPLACE_WITH_YOUR_API_KEY")
                .environment(Environment.SANDBOX)
                .build();
    }
}
```

/// Build the payment request

Construct a request object that contains all the necessary fields to process a transaction. Include payment details, a payment method, and customer data.

```java focus=23-52
// PayabliExample.java
package example;

import io.github.payabli.api.PayabliApiClient;
import io.github.payabli.api.PayabliApiClientBuilder;
import io.github.payabli.api.resources.moneyin.requests.RequestPaymentV2;
import io.github.payabli.api.types.V2TransactionResponseWrapper;
import io.github.payabli.api.types.TransRequestBody;
import io.github.payabli.api.types.PaymentDetail;
import io.github.payabli.api.types.PaymentMethod;
import io.github.payabli.api.types.PayMethodCredit;
import io.github.payabli.api.types.PayMethodCreditMethod;
import io.github.payabli.api.types.PayorDataRequest;
import io.github.payabli.api.core.Environment;

public class PayabliExample {
    public static void main(String[] args) {
        PayabliApiClient client = new PayabliApiClientBuilder()
                .apiKey("REPLACE_WITH_YOUR_API_KEY")
                .environment(Environment.SANDBOX)
                .build();

        PaymentDetail paymentDetails = PaymentDetail.builder()
                .totalAmount(100.0)
                .serviceFee(0.0)
                .build();

        PayorDataRequest customerData = PayorDataRequest.builder()
                .customerId(4440L)
                .build();

        PayMethodCredit paymentMethod = PayMethodCredit.builder()
                .cardexp("02/27")
                .cardnumber("4111111111111111")
                .method(PayMethodCreditMethod.CARD)
                .cardcvv("999")
                .cardHolder("Kassiane Cassian")
                .cardzip("12345")
                .initiator("payor")
                .build();

        TransRequestBody requestBody = TransRequestBody.builder()
                .paymentDetails(paymentDetails)
                .paymentMethod(PaymentMethod.of(paymentMethod))
                .customerData(customerData)
                .entryPoint("REPLACE_WITH_YOUR_ENTRYPOINT")
                .ipaddress("255.255.255.255")
                .build();

        RequestPaymentV2 request = RequestPaymentV2.builder()
                .body(requestBody)
                .build();
    }
}
```

/// Execute the transaction

Call `moneyIn().getpaidv2()` to process the transaction. The client calls Payabli's `POST /v2/MoneyIn/getpaid` endpoint.

```java focus=54
// PayabliExample.java
package example;

import io.github.payabli.api.PayabliApiClient;
import io.github.payabli.api.PayabliApiClientBuilder;
import io.github.payabli.api.resources.moneyin.requests.RequestPaymentV2;
import io.github.payabli.api.types.V2TransactionResponseWrapper;
import io.github.payabli.api.types.TransRequestBody;
import io.github.payabli.api.types.PaymentDetail;
import io.github.payabli.api.types.PaymentMethod;
import io.github.payabli.api.types.PayMethodCredit;
import io.github.payabli.api.types.PayMethodCreditMethod;
import io.github.payabli.api.types.PayorDataRequest;
import io.github.payabli.api.core.Environment;

public class PayabliExample {
    public static void main(String[] args) {
        PayabliApiClient client = new PayabliApiClientBuilder()
                .apiKey("REPLACE_WITH_YOUR_API_KEY")
                .environment(Environment.SANDBOX)
                .build();

        PaymentDetail paymentDetails = PaymentDetail.builder()
                .totalAmount(100.0)
                .serviceFee(0.0)
                .build();

        PayorDataRequest customerData = PayorDataRequest.builder()
                .customerId(4440L)
                .build();

        PayMethodCredit paymentMethod = PayMethodCredit.builder()
                .cardexp("02/27")
                .cardnumber("4111111111111111")
                .method(PayMethodCreditMethod.CARD)
                .cardcvv("999")
                .cardHolder("Kassiane Cassian")
                .cardzip("12345")
                .initiator("payor")
                .build();

        TransRequestBody requestBody = TransRequestBody.builder()
                .paymentDetails(paymentDetails)
                .paymentMethod(PaymentMethod.of(paymentMethod))
                .customerData(customerData)
                .entryPoint("REPLACE_WITH_YOUR_ENTRYPOINT")
                .ipaddress("255.255.255.255")
                .build();

        RequestPaymentV2 request = RequestPaymentV2.builder()
                .body(requestBody)
                .build();

        V2TransactionResponseWrapper result = client.moneyIn().getpaidv2(request);
    }
}
```

/// Show the result

Print key fields from the response to confirm the transaction succeeded.

```java focus=56-58
// PayabliExample.java
package example;

import io.github.payabli.api.PayabliApiClient;
import io.github.payabli.api.PayabliApiClientBuilder;
import io.github.payabli.api.resources.moneyin.requests.RequestPaymentV2;
import io.github.payabli.api.types.V2TransactionResponseWrapper;
import io.github.payabli.api.types.TransRequestBody;
import io.github.payabli.api.types.PaymentDetail;
import io.github.payabli.api.types.PaymentMethod;
import io.github.payabli.api.types.PayMethodCredit;
import io.github.payabli.api.types.PayMethodCreditMethod;
import io.github.payabli.api.types.PayorDataRequest;
import io.github.payabli.api.core.Environment;

public class PayabliExample {
    public static void main(String[] args) {
        PayabliApiClient client = new PayabliApiClientBuilder()
                .apiKey("REPLACE_WITH_YOUR_API_KEY")
                .environment(Environment.SANDBOX)
                .build();

        PaymentDetail paymentDetails = PaymentDetail.builder()
                .totalAmount(100.0)
                .serviceFee(0.0)
                .build();

        PayorDataRequest customerData = PayorDataRequest.builder()
                .customerId(4440L)
                .build();

        PayMethodCredit paymentMethod = PayMethodCredit.builder()
                .cardexp("02/27")
                .cardnumber("4111111111111111")
                .method(PayMethodCreditMethod.CARD)
                .cardcvv("999")
                .cardHolder("Kassiane Cassian")
                .cardzip("12345")
                .initiator("payor")
                .build();

        TransRequestBody requestBody = TransRequestBody.builder()
                .paymentDetails(paymentDetails)
                .paymentMethod(PaymentMethod.of(paymentMethod))
                .customerData(customerData)
                .entryPoint("REPLACE_WITH_YOUR_ENTRYPOINT")
                .ipaddress("255.255.255.255")
                .build();

        RequestPaymentV2 request = RequestPaymentV2.builder()
                .body(requestBody)
                .build();

        V2TransactionResponseWrapper result = client.moneyIn().getpaidv2(request);

        System.out.println("Reason: " + result.getReason());
        System.out.println("Transaction ID: " + result.getData().getPaymentTransId());
        System.out.println("Auth code: " + result.getData().getResponseData().getAuthcode().orElse(null));
    }
}
```

Generate the Gradle wrapper so you don't need a system-wide Gradle installation:

```bash
gradle wrapper
```

Compile and run the app with the Gradle wrapper:

```bash
./gradlew clean run
```

Check the console output for the result of the transaction.
A successful transaction returns output like this:

```txt
    Reason: Approved
    Transaction ID: 255-9242c1dd69ac44f292c44be7a995e8b1
    Auth code: TAS815
```

In production, we recommend that you pass a stored method ID to the `paymentMethod` object instead of card details.
See more information in [Tokenization Overview](/guides/platform-tokenization-overview).

## SDK example app

The SDK example app is a basic web app built with the Payabli Java SDK.
It shows how to manage customers and use the temporary token flow with the SDK.
The code is publicly available in the [example repository](https://github.com/payabli/examples/tree/main/sdk/java-sdk).

<img src="https://files.buildwithfern.com/payabli.docs.buildwithfern.com/d440856d350fd1d5a41c108ca9145534f79f17f2e3a0bca8038ba370ee594b83/images/sdk-example-app.png" alt="customer creation page of SDK example app" />

### Set up the app

Follow these steps to set up the SDK example app on your local machine:

Open your terminal and run the following command to clone the SDK example app repository:

```bash
git clone https://github.com/payabli/examples
```

In your terminal, navigate to the directory containing the SDK example app:

```bash
cd examples/sdk/java-sdk
```

Install the SDK and dependencies:

```bash
mvn clean install
```

Copy the`.env.template` file to a new file called `.env`:

```bash
cp .env.template .env
```

Open the `.env` file in your code editor. Set `PAYABLI_KEY` to a private API token, `PAYABLI_ENTRY` to your Payabli entrypoint, and `PAYABLI_PUBLIC_TOKEN` to a public API token:

```bash
  # your Payabli Private API token
  PAYABLI_KEY="o.Oim...Mekgjw="
  # your Payabli entrypoint
  PAYABLI_ENTRY="41xxxxxa7e"
  # your Payabli Public API token
  PAYABLI_PUBLIC_TOKEN="o.Oim...Mekgjw="
```

Run this command to start the development server, and open the app in your browser:

```bash
mvn exec:java
```

### Use the app

The SDK example app has three pages:

1. **Create Customer** - Create a new customer in the Payabli entrypoint.
2. **List Customers** - View a list of all customers in the Payabli entrypoint.
3. **Make Transaction** - Make a transaction using the temporary token flow.

#### Create customer

The **Create Customer** page has a form that allows you to create a new customer.
Fill in the form with the customer's information and click the "Create" button.
If the customer is created successfully, a green success message appears below the button.

#### List customers

The **List Customers** page has a table of all customers in the entrypoint.
You can view the customer's information, including their name, email address, and ZIP Code.
Click the "X" button on the right side of a customer's row to delete the customer.
If the customer is deleted successfully, the row is removed from the table.

#### Make transaction

The **Make Transaction** page contains an EmbeddedMethod UI component using the temporary token flow.
Fill in the form with payment information and click the "Process" button when the payment information is valid.
The app performs these steps:

1. The embedded component saves the payment method as a temporary token and sends it to the server.
2. The server converts the temporary token to a permanent token with the **POST TokenStorage/add** endpoint.
3. The server uses the permanent token to make a transaction with the **POST /v2/MoneyIn/getpaid** endpoint.

If everything is successful, a green success message appears below the embedded component.
See [Extend embedded components with the temporary token flow](/guides/platform-developer-tokenization-temp-flow) for more information.