Create & Register Vehicle

View as Markdown
# Create & Register Vehicle Registers a new vehicle record (make, model, color, license number plate) and links it to a driver. If the request includes a driver `Bearer` token in the `Authorization` header, the vehicle is automatically associated with that driver without needing to pass `driverId` in the body. --- ### Endpoint Overview - **Method:** `POST` - **Route:** `/api/v1/vehicles` - **Authentication:** `Optional Bearer Token (Driver JWT)` or via `driverId` in body - **Content-Type:** `application/json` --- ### Request Headers | Header | Type | Required | Description | | --- | --- | --- | --- | | `Authorization` | `string` | No | Optional Format: `Bearer` (auto-links vehicle to authenticated driver) | | `Content-Type` | `string` | **Yes** | Must be `application/json` | --- ### Request Body Schema | Field | Data Type | Required | Nullable | Allowed Values / Constraints | Description | | --- | --- | --- | --- | --- | --- | | `name` | `string` | **Yes** | No | Min length 1 (e.g. `"Maruti Suzuki"`, `"Honda"`, `"Bajaj"`) | Vehicle manufacturer / brand make | | `model` | `string` | **Yes** | No | Min length 1 (e.g. `"Swift Dzire"`, `"Activa 6G"`, `"Maxima"`) | Vehicle model name | | `driverId` | `string` | No | Yes | Prefixed with `drv_` | ID of driver to link (auto-filled if Bearer token present) | | `color` | `string` | No | Yes | e.g. `"White"`, `"Silver"`, `"Black"` | Vehicle paint color | | `number_plate` | `string` | No | Yes | e.g. `"KA-01-AB-1234"`, `"DL-04-XY-9876"` | Government registration plate number | #### Example Request Body ``` json { "name": "Maruti Suzuki", "model": "Swift Dzire", "color": "White", "number_plate": "KA-01-AB-1234" } ``` --- ### Response Body Schema (`201 Created`) | Top-Level Key | Data Type | Description | | --- | --- | --- | | `message` | `string` | Confirmation message (`"Vehicle created successfully"`) | | `vehicle` | `object` | The newly created vehicle record | #### `vehicle` Object Fields | Field | Data Type | Nullable | Allowed Values / Constraints | Description | | --- | --- | --- | --- | --- | | `id` | `string` | No | Prefixed with `veh_` | Unique vehicle ID | | `driverId` | `string` | Yes | Prefixed with `drv_` | Linked driver ID (or `null` if unlinked) | | `name` | `string` | No | String | Vehicle make / brand | | `model` | `string` | No | String | Vehicle model | | `color` | `string` | Yes | String | Paint color | | `number_plate` | `string` | Yes | String | Registration plate number | | `created_at` | `string` | No | ISO 8601 Timestamp | Record creation timestamp | | `updated_at` | `string` | No | ISO 8601 Timestamp | Last update timestamp | --- ### Response Examples #### 1\. `201 Created` — Vehicle Registered Successfully ``` json { "message": "Vehicle created successfully", "vehicle": { "id": "veh_1a2b3c4d5e", "driverId": "drv_8a7b6c5d4e", "name": "Maruti Suzuki", "model": "Swift Dzire", "color": "White", "number_plate": "KA-01-AB-1234", "created_at": "2026-09-21T05:40:00.000Z", "updated_at": "2026-09-21T05:40:00.000Z" } } ``` #### 2\. `400 Bad Request` — Validation Error (Missing Required Fields) ``` json { "success": false, "message": "Validation failed: name: Vehicle name is required, model: Vehicle model is required", "errors": [ { "field": "name", "message": "Vehicle name is required" }, { "field": "model", "message": "Vehicle model is required" } ] } ``` #### 3\. `500 Internal Server Error` ``` json { "error": "Failed to create vehicle" } ``` --- ### Status Codes Reference | HTTP Status Code | Condition | | --- | --- | | **`201 Created`** | Vehicle successfully inserted and linked in database. | | **`400 Bad Request`** | Missing `name` or `model` fields in request body. | | **`500 Internal Server Error`** | Server or database insertion error. |

Authentication

AuthorizationBearer

Bearer authentication of the form Bearer <token>, where token is your auth token.