Shopping Cart Abandonment (Java)
Next: Setup Hallmonitor

This tutorial walks you through how to use Bronto's REST API to abandon a shopping cart, which will trigger any cart abandonment workflows you may have.

Start Tutorial


In order to use Bronto's REST API to do shopping cart abandonment you need to have an account that uses Bronto's New Order Service. If you have the Cart Recovery app, you are using New Order Service. You also should have successfully set up your commerce configuration settings so that your site is connected to Bronto. All of your commerce configuration settings can be found and modified by logging into the platform and navigating to Home->Settings->Commerce.

If you want the platform to complete an action when you use the API to abandon a cart, your account will need to have a workflow that starts with a Cart Is Abandoned trigger and have created any assets that workflow needs, such as an email the workflow will send. You can create a workflow by logging into the platform and going to Automation->Workflows.

Finally, all of the Java code examples provided use the JAX-RS library. If you want to use the examples as written you will need to install the library before you start.

Setup Hallmonitor Client

If you haven't already, you need to set up Hallmonitor in order to be able to authenticate and work with Bronto's REST API. Hallmonitor is an OAuth 2.0 compliant service that is used to request and refresh access tokens for Bronto's APIs. You must create a client key and client secret for Hallmonitor in order to authenticate with Hallmonitor and get an API token.

  1. In the Bronto platform, navigate toHome > Settings > Data Exchange.
  2. Under the REST Integrations section, click Create New Integration.
  3. Enter a Client Name.
  4. Select the appropriate client permissions.
  5. Click Save.
  6. A client key (ID) and secret are generated and shown on the Data Exchange page. Save the client key and client secret shown in the REST Integrations section of the Data Exchange page. You will use it in the next step to create an API token.

    Import JAX-RS Library

    To use the example code provided, you will need to have installed the JAX-RS library. Import this library at the start of your session if you want to use our example code as written. Otherwise, import the library of your choice and adjust the code samples to work with your library.

    Set Global Variables

    Map the string variables you will use to construct requests and store responses in the following steps. This isn't strictly required, but it will save you from having to type these string variables multiple times.

    Because we are adding, abandoning, fiddling, and completing a Cart in this tutorial we've provided the variables for each of these calls. However, you may not need to perform all of these actions so only create the variables relevant to your situation. For example, if you only want to add a Cart you would not include the UPDATE_CART_PATH, ABANDON_CART_PATH, FIDDLE_CART_PATH, CART_COMPLETE_PATH, or CART_ID.

    Get Authentication Token

    This call returns an authentication token as well as a refresh token to be used once the access token expires. The access token expires after 1 hour. The refresh token expires after 30 days. You will need to use this token to access Bronto's API.

    Use Token To Authenticate With Bronto's API

    Each request to the external API must include an HTTP Authorization header. The header value should specify a “Bearer” authorization scheme followed by a space and then the OAuth2 access token.

    A 403 Forbidden response will be returned if no authorization header is provided, if a scheme other than Bearer is specified, if the token is unknown/expired, or if the token has an insufficient scope to execute the request.

    Add Cart

    In order to abandon a Cart, the Cart and its data must be stored in Bronto. If your Cart data is already in Bronto, skip to the next step. Otherwise, add your Cart to Bronto.

    When you add the Cart, first build the request data, making sure to match the Bronto defined names for each field. The example provided shows every valid field you can pass in for a Cart. Note, Carts in a COMPLETE or EXPIRED status cannot be updated or abandoned. If you are adding a new Cart you probably want it in the ACTIVE status, which is also the default status if none is provided.

    When you add the Cart, only set createContact to true if the Contact does not already exist in Bronto. If createContact is true and you provide an emailAddress that is not currently associated with a Contact, a transactional contact will be created. A transactional Contact can only be sent transactional emails until the Contact opts in to receiving messages from you, so make sure any message that is sent to this type of contact using the Cart Is Abandoned workflow has been approved for transactional sending.

    After you have added the Cart, check the response to make sure your request was successful. If it was not, use the information provided in the response to troubleshoot you issue. Generally, if you have an issue adding an order it's because the customerCartId already existed in Bronto or not all of the required data was included in the request. Grab the cartId from the response data to use in future requests.

    Abandon Cart

    Now that we have a Cart in Bronto, there a few ways it can be abandoned. When a Cart is added, a timer is started to keep track of how often Bronto sees any activity or updates on the Cart. Based on the rules defined by the user in the Settings page of the Cart Recovery App, Bronto can automatically mark the Cart as abandoned once it has been idle or unchanged for long enough.

    A Cart can also be abandoned directly by sending an Abandon Cart request as shown here.

    Fiddle Cart

    Fiddling a Cart is optional. As described in the previous step, when a Cart is added to Bronto a timer is started to keep track of often the Cart is updated. If you would like to tell Bronto that the Cart is still 'ACTIVE' and therefore should not be abandoned but don't have any actual changes to make on the Cart via an update, you can fiddle the Cart instead as show here. If the Cart is in the 'ABANDONED' state upon sending the fiddle request, the Cart will be moved back to the 'ACTIVE' status. Note, a Cart can be re-abandoned once it has been brought back to 'ACTIVE'.

    Complete Cart

    When your customer completes the checkout process on your site, you can complete the Cart in Bronto. This will mark the Cart and 'COMPLETE' and prevent any further updates or status changes. Note, when using the API, completing a Cart will not automatically create a corresponding Order. You must make an additional call to add an Order. See the Add or Update Orders tutorial for details on how to do that.

    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.UUID;
    public class AbandonCartExample {
    // Host
    private static final String BRONTO_HOST = "";
    // Paths - Includes Add Cart, Update Cart, and Abandon Cart because you will complete all of these actions in this tutorial.
    private static final String GET_TOKEN_PATH = "oauth2/token";
    private static final String ADD_CART_PATH = "carts";
    private static final String ABANDON_CART_PATH = "carts/{cartId}/abandon";
    private static final String FIDDLE_CART_PATH = "carts/{cartId}/fiddle";
    private static final String COMPLETE_CART_PATH = "carts/{cartId}/complete";
    private static final String CART_ID = "cartId";
    private static final String CREATE_CONTACT = "createContact";
    private static final String IGNORE_INVALID_TRACKING = "ignoreInvalidTracking";
    // OAuth Request property names
    private static final String GRANT_TYPE = "grant_type";
    private static final String CLIENT_ID = "client_id";
    private static final String CLIENT_SECRET = "client_secret";
    // OAuth Request property values
    private static final String CLIENT_CREDENTIALS = "client_credentials";
    private static final String EXAMPLE_CLIENT_ID = "xxxxxxxxxxxxxxxxxxxxx";
    private static final String EXAMPLE_CLIENT_SECRET = "xxxxxxxxxxxxxxxxxxxxx";
    private static final String ACCESS_TOKEN = "access_token";
    private static final String REASON_HEADER = "X-Reason";
    public static void main(String[] args) {
    Client client = ClientBuilder.newClient();
    // To be able to access Orders Rest API, you need an access token.
    // First, we build the request data needed to gain an access token
    Form requestData = new Form();
    requestData.param(GRANT_TYPE, CLIENT_CREDENTIALS);
    requestData.param(CLIENT_ID, EXAMPLE_CLIENT_ID);
    // Then build and send the request
    Response oauthResponse =