> [!NOTE]
> **Note:** **Before constructing a vehicle request** , read the requirements under [Vehicle requests](https://developers.google.com/maps/documentation/mobility/fleet-engine/essentials/vehicles.md.txt#vehicle_requests) in the Introduction.

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](https://developers.google.com/maps/documentation/mobility/driver-sdk/scheduled.md.txt) for scheduled tasks
- [Service account roles](https://developers.google.com/maps/documentation/mobility/fleet-engine/essentials/set-up-fleet/service-accounts.md.txt) 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`

> [!IMPORTANT]
> **Important:** Don't set any other fields. If you do, Fleet Engine returns an error because those fields are either read-only or can only be updated with a call to `UpdateDeliveryVehicle`.

To create a vehicle without setting any optional fields, you can leave the
`DeliveryVehicle` field unset in the `CreateDeliveryVehicleRequest`.

## Create vehicle example

To create a vehicle, you can use the [Java gRPC library](https://developers.google.com/maps/documentation/mobility/fleet-engine/essentials/client-libraries-tasks.md.txt#java) or REST. For a
complete list of supported languages, see
[Client libraries for scheduled task services](https://developers.google.com/maps/documentation/mobility/fleet-engine/essentials/client-libraries-tasks.md.txt).

### Java

```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`:

```s
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`

```s
  # 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:

```s
  # 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}"
```

## What's next

- [Update delivery vehicle fields](https://developers.google.com/maps/documentation/mobility/fleet-engine/essentials/vehicles/scheduled-tasks-vehicle-fields.md.txt)