# @chainsafe/is-ip

> Check if a string is an IP address

Latest version **2.1.0** (published 2025-01-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install @chainsafe/is-ip
pnpm add @chainsafe/is-ip
yarn add @chainsafe/is-ip
bun add @chainsafe/is-ip
```

## Health

**Score 50/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2025-01-30 |
| First published | 2022-10-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Dependencies | 0 |
| Unpacked size | 39.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | ChainSafe Systems |
| Maintainers | wemeetagain, mpetrunic |
| Keywords | ip, ipv6, ipv4, regex, regexp, match, test, find, text, pattern, internet, protocol, address, validate, detect, check, is, string |

## Links

- npm: https://www.npmjs.com/package/@chainsafe/is-ip
- Repository: https://github.com/ChainSafe/is-ip
- Homepage: https://github.com/ChainSafe/is-ip#readme
- Issues: https://github.com/ChainSafe/is-ip/issues
- npm.io page: https://npm.io/package/@chainsafe/is-ip

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 2.1.0 (latest) — 2025-01-30
- 2.0.2 — 2023-08-11
- 2.0.1 — 2022-10-29
- 2.0.0 — 2022-10-29
- 1.0.0 — 2022-10-29

## README

# @chainsafe/is-ip

> Check if a string is an IP address

## Install

```bash
npm install @chainsafe/is-ip
```

## Example

Check if a string is an IP address

```typescript
import { expect } from "chai";
import {
	isIPv4,
	isIPv6,
	isIP,
	ipVersion,
} from "@chainsafe/is-ip";

// check if a string is a valid IPv4
expect(isIPv4("127.0.0.1")).to.equal(true);

// check if a string is a valid IPv6
expect(isIPv6("127.0.0.1")).to.equal(false);

// check if a string is a valid IPv4 or IPv6
expect(isIP("127.0.0.1")).to.equal(true);

// return 4, 6, or undefined
expect(ipVersion("127.0.0.1")).to.equal(4);
expect(ipVersion("1:2:3:4:5:6:7:8")).to.equal(6);
expect(ipVersion("invalid ip")).to.equal(undefined);
```

Parse a string into IP address bytes

```typescript
import { expect } from "chai";
import {
	parseIPv4,
	parseIPv4Mapped,
	parseIPv6,
	parseIP,
} from "@chainsafe/is-ip/parse";

// parse a string into IPv4 bytes
const b1 = parseIPv4("127.0.0.1");
expect(b1).to.deep.equal(Uint8Array.from([127, 0, 0, 1]));

// parse a string into IPv6 bytes
const b2 = parseIPv6("::1");
expect(b2).to.deep.equal(Uint8Array.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]));

// parse an IPv4 string into IPv4-mapped IPv6 bytes
const b3 = parseIPv4Mapped("127.0.0.1");
expect(b3).to.deep.equal(Uint8Array.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 127, 0, 0, 1]));

// parse a string into either IPv4 or IPv6 bytes
const b4 = parseIP("127.0.0.1");
expect(b4).to.deep.equal(Uint8Array.from([127, 0, 0, 1]));

const b5 = parseIP("::1");
expect(b5).to.deep.equal(Uint8Array.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]));

const b6 = parseIP("127.0.0.1", true);
expect(b6).to.deep.equal(Uint8Array.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 127, 0, 0, 1]));

// parseIP* functions return undefined on invalid input
expect(parseIP("not an IP")).to.equal(undefined);
```

## License

MIT

---
_Source: https://npm.io/package/@chainsafe/is-ip · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
