Payments with the Java library

Learn to create a payment with our Java SDK.

You can create a payment in your backend application with our Java library. We recommend using the Java library over a custom integration with Java, as it's quicker and easier to maintain.

For installation and general information about our Java SDK, visit the TrueLayer GitHub.


Check out the Payments API v3 reference

Refer to the API reference for further information on creating a payment

Before you start

Initialise the client

You now have all the information required to initialise the Java library. Make sure that you set the environment to .sandbox when testing your integration as shown in the following code sample.

TrueLayerClient client = TrueLayerClient.New()

To switch from sandbox to live, remove the .environment(...) selector.

The client builder should give you enough flexibility to load your configuration from your filesystem or environment variables. See further examples in our GitHub repository.

Create a payment

Our Java library mimics Payments V3 API. See our API reference to learn more about payment initiation. The following code snippet can be used for reference:

CreatePaymentRequest createPaymentRequest = CreatePaymentRequest.builder()
            .email("[email protected]")

ApiResponse<CreatePaymentResponse> createdPayment= tlClient.payments()

    //error scenario branch...
    ProblemDetails problemDetails = createdPaymentResponse.getError();

    // Inspect the Problem details error and return

// build a link to our Hosted Payment Page
URI url = client.hpp().getHostedPaymentPageLink(

return url;

Authorise a payment

After a successful /payments request, you can authorise a payment by:

Hosted payment page

The simplest option for web developers is to redirect to our hosted payment page. The Java library provides a utility to create a link to that page:

CreatePaymentResponse createdPayment = createPaymentResponse.getData();

URI hppUrl = client.hpp().getHostedPaymentPageLink(

See our hosted payment page documentation for further details.

Mobile SDKs

To move on with the payment authorization on TrueLayer mobile SDKs, you can return the id and resourceToken fields of a CreatePaymentResponse instance to your mobile clients.

See our iOS SDK or Android SDK documentation for further details.

Direct API integration

If you want to have a fully customised journey, either on mobile or web applications, you can perform the authorisation steps on your server-side component. Refer to the startAuthorizationFlow and submitProviderSelection parts of the Java library for further details.

This is a sample implementation:

// start the auth flow
StartAuthorizationFlowRequest startAuthorizationFlowRequest = StartAuthorizationFlowRequest.builder()
ApiResponse<StartAuthorizationFlowResponse> startAuthorizationFlowResponse = tlClient.payments()
    .startAuthorizationFlow(createPaymentResponse.getData().getId(), startAuthorizationFlowRequest)

    //error scenario branch...
    ProblemDetails problemDetails = startAuthorizationFlowResponse.getError();
    //return the error details...

// Submit the provider selection
ApiResponse<SubmitProviderSelectionResponse> submitProviderSelectionResponseResponse = tlClient.payments()

    //error scenario branch...
    ProblemDetails problemDetails = submitProviderSelectionResponse.getError();
    //return the error details...

// Get the redirect link to the bank and return it to your client
URI bankUrl = submitProviderSelectionResponseResponse.getData()