Create a payment using Java

Learn how to make a payment initiation request using our Java SDK.

With our Java SDK, you can create a payment on your backend application. We recommend using the Java SDK over a custom integration with Java, as it will reduce the time to make your first payment.

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


Check out the API reference for Payments API v3

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

Before you start

  1. Set up your account on Console.
  2. Configure request signing.

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.
When you switch to live, you can simply remove the .environment(...) selector.

TrueLayerClient client = TrueLayerClient.New()

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

Create a payment

Our Java library mimics the Payments V3 API. See our API reference to learn more about payment initiation. The following snippet can be used as 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

With the successful result of a /payments request, you can complete a payment by:

Hosted Payment Page

The easiest option for web developers is to redirect to our Hosted Payment Page. The Java library provides a utility to easily 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.

Custom integration

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

A sample implementation is as follows:

// 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()