# BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters

**Code:** `BSONError: input must be a 24 character hex string` · **Category:** MongoDB · **Seen on:** MongoDB, Node.js

`new ObjectId(value)` received a value that isn't a valid 24-hex id.

## Meaning

IDs from URLs/forms may be undefined, `"undefined"`, or numeric. Validate with `ObjectId.isValid(id)` before constructing and return 400/404 for invalid ones. Mongoose's equivalent is CastError for `_id`.

## Common causes

- Undefined/null id from the client
- Id with whitespace or wrong length
- Using numeric/UUID ids with ObjectId
- Mixed up id formats between systems
- Route param typo

## Quick fix

1. `if (!ObjectId.isValid(id)) return res.status(400).end()`
2. Trim and validate input
3. Use the right id type for the collection

## Detailed fix by platform

### Node.js

1. `const { ObjectId } = require("mongodb"); ObjectId.isValid("64b9f0c1e4b0a1b2c3d4e5f6") // true`

## How to diagnose

1. **Value** — Valid 24 hex?
2. **Source** — Client input?
3. **Type** — ObjectId vs string?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/mongodb/bsonerror-invalid-objectid-24-hex)
