
In our example use case, we will go through how to use the API to request the state of a vehicle.  In each step, bash and postman examples will be given.

### Authenticate

First, we use the authentication endpoint `/token` to retrieve our bearer token for use in subsequent requests.

![Authenticate Using Postman](../../assets/token_request_postman.png)

```bash
curl -X POST  --data "clientId=${clientId}" --data "clientSecret=${clientSecret}" https://$AUTH_HOST/vehicle-status-api/token
```
Response:
```Json
{
  "access_token": "example",
  "token_type": "Bearer",
  "expires_in": "1744206587345"
}
```

Now that we have our bearer token, we can use it in subsequent requests.

### Understanding Resource Identifiers

Our Ford Telematics APIs require a specific resource identifier based on the data requested:
- **Vehicle-based APIs**: Require a [VehicleID](#obtaining-a-vehicleid).
- **Driver-based APIs**: Require a [DriverID](#obtaining-a-driverid).

#### Obtaining a VehicleID

To obtain a VehicleID for your enrolled vehicles, use the [Vehicle Metadata](./API%20Endpoints/Vehicle%20Metadata) endpoint:

```bash
curl -X GET \
  -H "Authorization: Bearer example" \
  "https://${HOST}/vehicle-status-api/v3/vehicles"
```

Response:
```Json
{
  "content": [
    {
      "vehicleUuid": "550e8400-e29b-41d4-a716-446655440000",
      "vin": "1FMEU111111111111",
      "make": "Ford",
      "model": "F-150",
      "year": 2023
    }
  ],
  "page": {
    "number": 1,
    "size": 100,
    "totalElements": 1,
    "totalPages": 1
  }
}
```

The `vehicleUuid` field in the response is the VehicleID you'll use in vehicle-based API requests.

#### Obtaining a DriverID

To obtain a DriverID for your drivers, use the [Users](./API%20Endpoints/Users) endpoint with the `DRIVER` role filter:

```bash
curl -X POST \
  -H "Authorization: Bearer example" \
  -H "Content-Type: application/json" \
  "https://${HOST}/vehicle-status-api/v3/users/search" \
  -d '{
    "role": "DRIVER"
  }'
```

Response:
```Json
{
  "content": [
    {
      "userId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
      "email": "driver@email.com",
      "firstName": "John",
      "lastName": "Doe",
      "countryCode": "USA",
	    "applicationNames": [
	      "fordTelematics",
	      "in_cab_coaching",
	      "administrativeConsole",
	      "developer_portal"
	    ]
    }
  ],
  "page": {
    "number": 1,
    "size": 100,
    "totalElements": 1,
    "totalPages": 1
  }
}
```

The `userId` field in the response is the DriverID you'll use in driver-based API requests.

### Request a Vehicle Status

We will use our bearer token and example vehicleId `550e8400-e29b-41d4-a716-446655440000` to query the `vehicle-status-api/v2/vehicles/:vehicleId/status` endpoint. We will also use the `signalFilter` request parameter to limit the resulting signals to position and odometer.  The signalFilter parameter is optional and if not included as part of the request, the response will contain all signals valid for the vehicles fuel type.

![Vehicle Status Using Postman](../../assets/status_request_postman.png)

```bash
curl -X GET \
-H "Authorization: Bearer example" \
"https://${HOST}/vehicle-status-api/v2/vehicles/550e8400-e29b-41d4-a716-446655440000/status?signalFilter=position,odometer"
```
Response:
```Json
{
  "vin": "1FMEU111111111111",
  "vehicleId": "550e8400-e29b-41d4-a716-446655440000",
  "fuelType": "Gas",
  "signals": [
    {
      "position": {
        "timestamp": "2023-01-26T22:01:01.663Z",
        "value": {
          "latitude": 42.296436,
          "longitude": -83.207819
        }
      }
    },
    {
      "odometer": {
        "timestamp": "2023-01-26T22:01:01.663Z",
        "value": 24000.0
      }
    }
  ]
}
```

### Notes on FuelType

The signals returned from the status API are dependent on the fuel type of the vehicle.  Below is a table outlining what signals can be expected for each fuel type.

|                                   | Gas | Electric | Diesel | CompressedNaturalGas | FlexFuel | Hybrid | PluginHybrid |
|-----------------------------------|-----|----------|--------|----------------------|----------|--------|--------------|
| position                          | x   | x        | x      | x                    | x        | x      | x            |
| odometer                          | x   | x        | x      | x                    | x        | x      | x            |
| ignition_status                   | x   | x        | x      | x                    | x        | x      | x            |
| oil_life_remaining                | x   |          | x      | x                    | x        | x      | x            |
| fuel_level                        | x   |          | x      | x                    | x        | x      | x            |
| total_engine_time                 | x   |          | x      | x                    | x        | x      | x            |
| battery_voltage                   | x   | x        | x      | x                    | x        | x      | x            |
| xev_battery_state_of_charge       |     | x        |        |                      |          | x      | x            |
| xev_battery_range                 |     | x        |        |                      |          | x      | x            |
| xev_plug_charger_status           |     | x        |        |                      |          |        | x            |
| xev_battery_charge_display_status |     | x        |        |                      |          |        | x            |
| heading                           | x   | x        | x      | x                    | x        | x      | x            |

If FuelType is not found, all signals will be returned. Fuel type is currently not available for non-Ford vehicles.

### Request Vehicle Health Alert Diagnostic Processed Events

We will use our bearer token and example vehicleId `550e8400-e29b-41d4-a716-446655440000` to query the `vehicle-status-api/v2/vehicles/:vehicleId/health` endpoint.

See the [API Reference](./API%20Endpoints/Health#GET%20/v2/vehicles/:vehicleId/health) for more details.

```bash
curl -X GET -H "Authorization: Bearer example" "https://${HOST}/vehicle-status-api/v2/vehicles/550e8400-e29b-41d4-a716-446655440000/health"
```
Response:
```Json
{
  "vin": "1FMEU111111111111",
  "signals": [
    {
      "tag": {
          "symptom": "Active park assist may not activate.  Active park assist switch has a fault.",
          "severity": "LOW",
          "category": "Restraints & Driver Assistance",
          "action": "Have the parking aid system checked by an authorized dealer as soon as possible."
      },
      "indicator_light": {
          "wellKnownIndicator": "PARK_AID_MALFUNCTION",
          "indicatorState": "ON",
          "value": "600E01"
      },
      "dtcCode": "B129E",
      "occurredAt": "2022-02-18T14:45:01Z"
    }
  ]
}
```

### Request Historical Vehicle Signals

We will use our bearer token and example vehicleId `550e8400-e29b-41d4-a716-446655440000` to query the `vehicle-status-api/v1/vehicles/:vehicleId/historical` endpoint.  This endpoint has two required get parameters, `startAt` and `endAt`.
All signals returned by this endpoint will have been emitted between the `startAt` and `endAt` times. In this example we will query for a one hour window.  We will also set the page `size` to three signals, though for practical uses a larger `size` is recommended.

See the [API Reference](./API%20Endpoints/History#GET%20/v2/vehicles/:vehicleId/historical) for more details.

```bash
curl -X GET \
  -H "Authorization: Bearer example" \
  "https://${HOST}/vehicle-status-api/v2/vehicles/550e8400-e29b-41d4-a716-446655440000/historical?startAt=2023-02-01T14:00:00Z&endAt=2023-02-01T15:00:00Z&size=3"
```
Response:
```Json
{
  "content" : [
      {
        "vin": "1FMEU111111111111",
        "fuelType": "Gas",
        "signal": {
          "type": "position",
          "value": {
              "latitude": 1.0,
              "longitude": 2.0
          },
          "timestamp": "2023-02-01T14:55:30Z"
        }
      },
      {
        "vin": "1FMEU111111111111",
        "fuelType": "Gas",
        "signal": {
          "type": "odometer",
          "value": 1099.15,
          "timestamp": "2023-02-01T14:55:30Z"
        }
      },
      {
        "vin": "1FMEU111111111111",
        "fuelType": "Gas",
        "signal": {
          "type": "speed",
          "value": 15.6,
          "timestamp": "2023-02-01T14:55:30Z"
        }
      }
  ],
  "page": {
    "cursor": null,
    "size": 3,
    "nextCursor": "2:1687842896214",
  }
}


```

Note that the `signals` list displays signals in order of `timestamp` descending.

To retrieve the next page of data, we use the value of the `nextcursor` key as the `page` parameter in our request.

```bash
curl -X GET \
-H "Authorization: Bearer example" \
"https://${HOST}/vehicle-status-api/v2/vehicles/550e8400-e29b-41d4-a716-446655440000/historical?startAt=2023-02-01T14:00:00Z&endAt=2023-02-01T15:00:00Z&size=3&page=2:1687842896214"
```
Response:
```Json
{
  "content" : [
      {
        "vin": "1FMEU111111111111",
        "fuelType": "Gas",
        "signal": {
          "type": "position",
          "value": {
              "latitude": 1.0,
              "longitude": 2.0
          },
          "timestamp": "2023-02-01T14:55:30Z"
        }
      },
      {
        "vin": "1FMEU111111111111",
        "fuelType": "Gas",
        "signal": {
          "type": "odometer",
          "value": 1099.15,
          "timestamp": "2023-02-01T14:55:30Z"
        }
      },
      {
        "vin": "1FMEU111111111111",
        "fuelType": "Gas",
        "signal": {
          "type": "speed",
          "value": 15.6,
          "timestamp": "2023-02-01T14:55:30Z"
        }
      }
  ],
  "page": {
    "cursor": "2:1687842896214",
    "size": 3,
    "nextCursor": "9:1687826493602"
  }
}
```