AI-generated Key Takeaways
- 
          This documentation outlines how to create a vehicle using gRPC or REST from a server environment or the Driver SDK (if provisioned as a trusted environment). 
- 
          When creating a vehicle, you can optionally set attributes,last_location, andtypefields using theCreateDeliveryVehiclerequest.
- 
          Creating a vehicle requires a CreateDeliveryVehicleRequestobject with a defined parent, vehicle ID, and optional vehicle attributes.
- 
          Fleet Engine provides libraries and endpoints for vehicle creation using Java gRPC or REST API calls with necessary authorization. 
- 
          To avoid errors, do not set read-only or updatable fields when creating the vehicle; these can be modified using UpdateDeliveryVehicle.
This document describes how to create a vehicle from a server environment using either gRPC or REST. You can create a vehicle from the Driver SDK, provided you have provisioned the app as a trusted environment using the appropriate credentials.
To understand how to use the Driver SDK to create vehicles, see the following:
- Driver SDK for scheduled tasks
- Service account roles under Fleet Engine essentials.
To create a new vehicle from a server environment, make a
CreateDeliveryVehicle request to Fleet Engine. Use the
CreateDeliveryVehicleRequest object to define the attributes of the new
delivery vehicle.
Fields for scheduled tasks vehicles
When creating a DeliveryVehicle, you set the following optional fields:
- attributes
- last_location
- type
To create a vehicle without setting any optional fields, you can leave the
DeliveryVehicle field unset in the CreateDeliveryVehicleRequest.
Create vehicle example
You can use the Java gRPC library to create a vehicle, or REST.
Java
  static final String PROJECT_ID = "my-delivery-co-gcp-project";
  static final String VEHICLE_ID = "vehicle-8241890"; // Avoid auto-incrementing IDs.
  DeliveryServiceBlockingStub deliveryService =
    DeliveryServiceGrpc.newBlockingStub(channel);
  // Vehicle settings
  String parent = "providers/" + PROJECT_ID;
  DeliveryVehicle vehicle = DeliveryVehicle.newBuilder()
    .addAttributes(DeliveryVehicleAttribute.newBuilder()
      .setKey("route_number").setValue("1"))  // Opaque to the Fleet Engine
    .build();
  // Vehicle request
  CreateDeliveryVehicleRequest createVehicleRequest =
    CreateDeliveryVehicleRequest.newBuilder()  // No need for the header
        .setParent(parent)
        .setDeliveryVehicleId(VEHICLE_ID)     // Vehicle ID assigned by the Provider
        .setDeliveryVehicle(vehicle)
        .build();
  // Error handling
  // If Fleet Engine does not have vehicle with that ID and the credentials of the
  // requestor pass, the service creates the vehicle successfully.
  try {
    DeliveryVehicle createdVehicle =
      deliveryService.createDeliveryVehicle(createVehicleRequest);
  } catch (StatusRuntimeException e) {
    Status s = e.getStatus();
    switch (s.getCode()) {
       case ALREADY_EXISTS:
         break;
       case PERMISSION_DENIED:
         break;
    }
    return;
  }
REST
To create a vehicle from a server environment, make an HTTP REST call
to CreateDeliveryVehicle:
POST https://fleetengine.googleapis.com/v1/providers/<project_id>/deliveryVehicles?deliveryVehicleId=<id>
The POST body represents the DeliveryVehicle entity to be created. You can
specify the following optional fields:
- attributes
- lastLocation
- type
  # Set $JWT, $PROJECT_ID, and $VEHICLE_ID in the local
  # environment
  curl -X POST "https://fleetengine.googleapis.com/v1/providers/${PROJECT_ID}/deliveryVehicles?deliveryVehicleId=${VEHICLE_ID}" \
    -H "Content-type: application/json" \
    -H "Authorization: Bearer ${JWT}" \
  --data-binary @- << EOM
  {
    "attributes": [{"key": "model", "value": "sedan"}],
    "lastLocation": {"location": {"latitude": 12.1, "longitude": 14.5}}
  }
  EOM
To create a vehicle without setting any fields, leave the body of the POST
request empty. The newly-created vehicle then extracts a vehicle ID from the
deliveryVehicleId parameter in the POST URL.
Example:
  # Set $JWT, $PROJECT_ID, and $VEHICLE_ID in the local
  # environment
  curl -X POST "https://fleetengine.googleapis.com/v1/providers/${PROJECT_ID}/deliveryVehicles?deliveryVehicleId=${VEHICLE_ID}" \
    -H "Content-type: application/json" \
    -H "Authorization: Bearer ${JWT}"