# nanoid

> This document is the Markdown version of [valibot.dev/api/nanoid/](https://valibot.dev/api/nanoid/). For the complete documentation index, see [llms.txt](https://valibot.dev/llms.txt).

Creates a [Nano ID](https://github.com/ai/nanoid) validation action.

```ts
const Action = v.nanoid<TInput, TMessage>(message);
```

## Generics

- `TInput` `extends string`
- `TMessage` `extends ErrorMessage<NanoIdIssue<TInput>> | undefined`

## Parameters

- `message` `TMessage`

### Explanation

With `nanoid` you can validate the formatting of a string. If the input is not an Nano ID, you can use `message` to customize the error message.

## Returns

- `Action` `NanoIdAction<TInput, TMessage>`

## Examples

The following examples show how `nanoid` can be used.

> Since Nano IDs are not limited to a fixed length, it is recommended to combine `nanoid` with [`length`](/api/length.md) to ensure the correct length.

### Nano ID schema

Schema to validate a Nano ID.

```ts
const NanoIdSchema = v.pipe(
  v.string(),
  v.nanoid('The Nano ID is badly formatted.'),
  v.length(21, 'The Nano ID must be 21 characters long.')
);
```

## Related

The following APIs can be combined with `nanoid`.

### Schemas

[`any`](/api/any.md), [`string`](/api/string.md), [`unknown`](/api/unknown.md)

### Methods

[`pipe`](/api/pipe.md)

### Utils

[`isOfKind`](/api/isOfKind.md), [`isOfType`](/api/isOfType.md)
