[API reference](https://evolu.dev/docs/api-reference) › [@evolu/common](https://evolu.dev/docs/api-reference/common) › [Type](https://evolu.dev/docs/api-reference/common/Type) › IpAddressFromString

```ts
const IpAddressFromString: transform(
  "IpAddressFromString",
  String,
  IpAddress,
  {
    from: (value: string): Result<IpAddress, IpAddressFromStringError> => {
      const address = value.includes(":")
        ? canonicalizeIpv6Address(value)
        : Ipv4Address.is(value)
          ? value
          : undefined;
      return address === undefined
        ? err({ type: "IpAddressFromString", value })
        : ok(address as IpAddress);
    },
    to: (value: IpAddress) => value,
  },
  formatIpAddressFromStringError,
);
```

Defined in: [packages/common/src/Type.ts:9553](https://github.com/evoluhq/evolu/blob/bc4578a260dd15d5bbf94b83765ca05984d194cb/packages/common/src/Type.ts#L9553)

Transforms IP address text into an [IpAddress](https://evolu.dev/docs/api-reference/common/Type/variables/IpAddress).

Text containing a colon is parsed as IPv6 text, as
[Ipv6AddressFromString](https://evolu.dev/docs/api-reference/common/Type/variables/Ipv6AddressFromString) parses it, and converted to its canonical
[Ipv6Address](https://evolu.dev/docs/api-reference/common/Type/variables/Ipv6Address). Other text must be an [Ipv4Address](https://evolu.dev/docs/api-reference/common/Type/variables/Ipv4Address), which has one
spelling. Because a colon selects IPv6, an IPv4 address with a port, such as
`1.2.3.4:80`, is rejected as invalid IPv6 text.

Encoding returns the canonical text, not the original spelling.

### Example

```ts
import {
  assertEqual,
  assertErr,
  assertOk,
  IpAddressFromString,
} from "@evolu/common";

assertOk(IpAddressFromString.fromUnknown("192.168.1.1"), "192.168.1.1");
assertOk(IpAddressFromString.fromUnknown("2001:0DB8::1"), "2001:db8::1");

const invalid = IpAddressFromString.fromUnknown("1.2.3.4:80");
assertErr(invalid, { type: "IpAddressFromString", value: "1.2.3.4:80" });
assertEqual(
  IpAddressFromString.formatError(invalid.error),
  'The value "1.2.3.4:80" is not a valid IP address.',
);
```