This document describes how to create a back-to-back trip, set the correct
fields, and assign it to a vehicle to fulfill. It assumes you have set up Fleet
Engine, you have created vehicles, have a working driver app, and
optionally, a consumer app. You should also be familiar with the various trip
scenarios available for on-demand trips. See the following related guides for
that:

- [Set up Fleet Engine](https://developers.google.com/maps/documentation/mobility/fleet-engine/essentials/set-up-fleet.md.txt)
- [Create a vehicle](https://developers.google.com/maps/documentation/mobility/fleet-engine/essentials/vehicles/on-demand-create-vehicle.md.txt)
- [Trip scenarios](https://developers.google.com/maps/documentation/mobility/fleet-engine/essentials/trip-intro.md.txt#trip-scenarios) in the **On-demand trips** overview

## Trip creation basics

This section describes the request details necessary for creating a trip in
Fleet Engine. You issue a creation request using either gRPC and REST.

- `CreateTrip()` method: [gRPC](https://developers.google.com/maps/documentation/mobility/fleet-engine/reference/trips/rpc/maps.fleetengine.v1.md.txt#maps.fleetengine.v1.TripService) or [REST](https://developers.google.com/maps/documentation/mobility/fleet-engine/reference/trips/rest/v1/providers.trips/create.md.txt)
- `CreateTripRequest` message: [gRPC](https://developers.google.com/maps/documentation/mobility/fleet-engine/reference/trips/rpc/maps.fleetengine.v1.md.txt#maps.fleetengine.v1.CreateTripRequest) only

> [!NOTE]
> **Note:** Using `CreateTrip()` requires the credentials for the privileged account mentioned in [Fleet Engine: Service account roles](https://developers.google.com/maps/documentation/mobility/fleet-engine/essentials/set-up-fleet/service-accounts.md.txt).

## Trip Fields

Use the following fields to create a trip in Fleet Engine. You can use different
fields for the different kinds of trips: single- or multi-destination,
back-to-back, or shared pooling trips. You
can supply the optional fields when you create the trip, or you can set them
later when you update the trip.

| Name | Required? | Description |
|---|---|---|
| **[parent](https://developers.google.com/maps/documentation/mobility/fleet-engine/reference/trips/rpc/maps.fleetengine.v1.md.txt#maps.fleetengine.v1.CreateTripRequest.FIELDS.string.maps.fleetengine.v1.CreateTripRequest.parent)** | Yes | A string that includes the project ID. This ID must be the same ID used across your entire Fleet Engine integration, with the same service account roles. |
| **[trip_id](https://developers.google.com/maps/documentation/mobility/fleet-engine/reference/trips/rpc/maps.fleetengine.v1.md.txt#maps.fleetengine.v1.CreateTripRequest.FIELDS.string.maps.fleetengine.v1.CreateTripRequest.trip_id)** | Yes | A string that you create that uniquely identifies this trip. Trip IDs have certain restrictions, as indicated in the reference. |
| **[trip_type](https://developers.google.com/maps/documentation/mobility/fleet-engine/reference/trips/rpc/maps.fleetengine.v1.md.txt#maps.fleetengine.v1.Trip.FIELDS.maps.fleetengine.v1.TripType.maps.fleetengine.v1.Trip.trip_type)** | Yes | Set the [TripType](https://developers.google.com/maps/documentation/mobility/fleet-engine/reference/trips/rpc/maps.fleetengine.v1.md.txt#triptype) to the following values for the trip type you're creating: - **Single destination** : Set to `SHARED` or `EXCLUSIVE`. - **Multi-destination** : Set to `EXCLUSIVE`. - **Back-to-back** : Set to `EXCLUSIVE`. - **Shared pooling** : Set to `SHARED`. |
| **[pickup_point](https://developers.google.com/maps/documentation/mobility/fleet-engine/reference/trips/rpc/maps.fleetengine.v1.md.txt#maps.fleetengine.v1.Trip.FIELDS.maps.fleetengine.v1.TerminalLocation.maps.fleetengine.v1.Trip.pickup_point)** | Yes | The trip's point of origin. |
| **[Intermediate destinations](https://developers.google.com/maps/documentation/mobility/fleet-engine/reference/trips/rpc/maps.fleetengine.v1.md.txt#maps.fleetengine.v1.Trip.FIELDS.repeated.maps.fleetengine.v1.TerminalLocation.maps.fleetengine.v1.Trip.intermediate_destinations)** | Yes | **Multi-destination trips only** : The list of intermediate destinations that the driver visits in between pickup and drop-off. As with `dropoff_point`, this field can also be set later by calling `UpdateTrip`, but a multi-destination trip by definition contains intermediate destinations. |
| **[vehicle_waypoints](https://developers.google.com/maps/documentation/mobility/fleet-engine/reference/trips/rpc/maps.fleetengine.v1.md.txt#maps.fleetengine.v1.Trip.FIELDS.repeated.maps.fleetengine.v1.TripWaypoint.maps.fleetengine.v1.Trip.vehicle_waypoints)** | Yes | **Shared-pooling trips only** : This field supports interleaving the waypoints from multiple trips. It contains all of the remaining waypoints for the assigned vehicle, as well as the pickup and drop-off waypoints for this trip. You can set this field by calling `CreateTrip` or `UpdateTrip`. You can also update vehicle waypoints through the `waypoints` field with a call to `UpdateVehicle`. The service does not return this information on `GetTrip` calls due to privacy reasons. |
| **[number_of_passengers](https://developers.google.com/maps/documentation/mobility/fleet-engine/reference/trips/rpc/maps.fleetengine.v1.md.txt#maps.fleetengine.v1.Trip.FIELDS.int32.maps.fleetengine.v1.Trip.number_of_passengers)** | No | The number of passengers on the trip. |
| **[dropoff_point](https://developers.google.com/maps/documentation/mobility/fleet-engine/reference/trips/rpc/maps.fleetengine.v1.md.txt#maps.fleetengine.v1.Trip.FIELDS.maps.fleetengine.v1.TerminalLocation.maps.fleetengine.v1.Trip.dropoff_point)** | No | The trip's destination. |
| **[vehicle_id](https://developers.google.com/maps/documentation/mobility/fleet-engine/reference/trips/rpc/maps.fleetengine.v1.md.txt#maps.fleetengine.v1.Trip.FIELDS.string.maps.fleetengine.v1.Trip.vehicle_id)** | No | The ID of the vehicle assigned to the trip. |
[Trip fields]

## Example: create a back-to-back trip

The following demonstrates how to create a back-to-back trip and assign it to a
vehicle. In this scenario, trip creation is the same as for a single-destination
trip. It's only later that you create another trip and assign it to a vehicle
with an already-active trip.

```java
// A vehicle with ID 'my-vehicle' is already created and it is assigned to a trip with ID 'current-trip'.

static final String PROJECT_ID = "my-rideshare-co-gcp-project";
static final String VEHICLE_ID =" my-vehicle";
static final String TRIP_ID = "back-to-back-trip");

TripServiceBlockingStub tripService = TripService.newBlockingStub(channel);

String parent = "providers/" + PROJECT_ID;

Trip trip = Trip.newBuilder()
    .setTripType(TripType.EXCLUSIVE)
    .setPickupPoint(
        TerminalLocation.newBuilder().setPoint(
            LatLng.newBuilder()
                .setLatitude(-6.195139).setLongitude(106.820826)))
    .setDropoffPoint(
        TerminalLocation.newBuilder().setPoint(
            LatLng.newBuilder()
                .setLatitude(-6.1275).setLongitude(106.6537)))
    .setVehicleId(VEHICLE_ID)
    .build();

// Create trip request
CreateTripRequest createTripRequest = CreateTripRequest.newBuilder()
    .setParent(parent)
    .setTripId(TRIP_ID)
    .setTrip(trip)
    .build();

// Error handling.
try {
  // Fleet Engine automatically assigns a 'waypoints' list to the trip containing
  // the vehicle's current trip waypoints.
  Trip createdTrip =
      tripService.createTrip(createTripRequest);
} catch (StatusRuntimeException e) {
  Status s = e.getStatus();
  switch (s.getCode()) {
    case ALREADY_EXISTS:
      break;
    case PERMISSION_DENIED:
      break;
  }
  return;
}
```

> [!WARNING]
> **Experimental:** As an experimental feature, the `TerminalLocation` ([gRPC](https://developers.google.com/maps/documentation/mobility/fleet-engine/reference/trips/rpc/maps.fleetengine.v1.md.txt#terminallocation) or [REST](https://developers.google.com/maps/documentation/mobility/fleet-engine/reference/trips/rest/v1/TerminalLocation.md.txt)) field now supports using `place` ([gRPC](https://developers.google.com/maps/documentation/mobility/fleet-engine/reference/trips/rpc/maps.fleetengine.v1.md.txt#maps.fleetengine.v1.TerminalLocation.FIELDS.string.maps.fleetengine.v1.TerminalLocation.place) or [REST](https://developers.google.com/maps/documentation/mobility/fleet-engine/reference/trips/rest/v1/TerminalLocation.md.txt#FIELDS.place)), either alongside or instead of `LatLng`.

## Update back-to-back trips

When you assign a vehicle for a back-to-back trip, you assign a trip to a
vehicle even if it has already been assigned one.

Any trip created in Fleet Engine must be assigned to a vehicle in order for
Fleet Engine to calculate trip ETAs and track it. You can do this either during
trip creation or later when you update the trip.

> [!NOTE]
> **Note:** If your server integration calls [`SearchVehicles`](https://developers.google.com/maps/documentation/mobility/fleet-engine/reference/trips/rpc/maps.fleetengine.v1.md.txt#maps.fleetengine.v1.VehicleService.SearchVehicles), you must set the `includeBackToBack` field of [`SearchVehiclesRequest`](https://developers.google.com/maps/documentation/mobility/fleet-engine/reference/trips/rpc/maps.fleetengine.v1.md.txt#maps.fleetengine.v1.SearchVehiclesRequest) as `true` so vehicles will be returned if they're assigned to a trip already.

After you assign the trip to a vehicle, Fleet Engine automatically adds the
waypoints associated with the back-to-back trips to the vehicle's waypoints
field. A trip's `remainingWaypoints` field contains a list of all of the
waypoints, including those from other trips that will be visited before
the trip's drop-off.

For example, consider two back-to-back trips: Trip A
and Trip B. The vehicle has picked up the consumer
for Trip A, and while en route to the drop-off
location, the driver receives a request to pickup another consumer for the
next trip, Trip B.

- Calling `getVehicle()` returns `remainingWaypoints` that contain:  
  A ~Drop-off~ → B ~Pickup~ → B ~Drop-off~.
- Either `getTrip()` or the `onTripRemainingWaypointsUpdated` callback for Trip A returns `remainingWaypoints` that contain:  
  A ~Drop-off~.
- Either `getTrip()` or the `onTripRemainingWaypointsUpdated` callback for Trip B returns `remainingWaypoints` that contain:   
  A ~Drop-off~ → B ~Pickup~ → and B ~Drop-off~.

## What's next

- [Create shared pooling trips](https://developers.google.com/maps/documentation/mobility/fleet-engine/journeys/trips/shared-pooling.md.txt)
- [Set up the Driver SDK](https://developers.google.com/maps/documentation/mobility/driver-sdk)