Getting Started

Learn how to parse, validate, and format Nigerian phone numbers with phoneng in under 5 minutes.

phoneng is a zero-dependency TypeScript library for handling Nigerian phone numbers. It parses any common input format, validates against the NCC prefix registry, and outputs normalized formats suitable for storage, display, and API integration.

Why phoneng?

Handling Nigerian phone numbers correctly is surprisingly complex:

  • Users enter numbers in many formats: 08031234567, +2348031234567, 234-803-123-4567
  • You need to store a canonical format but display a readable one
  • Network identification helps route SMS or detect carrier-specific issues
  • Manual regex solutions break on edge cases and are hard to maintain

phoneng solves all of this with a single function call.

Quick Start

Install the package:

npm install phoneng

Parse a phone number:

import { parse } from "phoneng";

const result = parse("08031234567");

if (result.valid) {
  console.log(result.e164); // +2348031234567
  console.log(result.national); // 08031234567
  console.log(result.network); // MTN
  console.log(result.type); // MOBILE
}

The parse function accepts any reasonable input format and returns a discriminated union. If parsing succeeds, you get all output formats and metadata. If it fails, you get a specific error code explaining what went wrong.

Input Formats

phoneng accepts all common Nigerian phone number formats:

InputDescription
08031234567National format with trunk prefix
8031234567Without trunk prefix
+2348031234567E.164 format
2348031234567Compact format
+234 803 123 4567International with spaces
0803-123-4567With dashes

All of these parse to the same canonical number. Whitespace, dashes, and parentheses are stripped before validation.

Output Formats

Every successful parse returns five output formats:

PropertyExampleUse Case
e164+2348031234567SMS APIs, international storage
national08031234567Display to Nigerian users
international+234 803 123 4567International display
compact2348031234567Paystack, Flutterwave
rfc3966tel:+2348031234567Click-to-call links

Choose the format that matches your use case. Store e164 in your database for consistency.

Next Steps

  • Installation for detailed setup including CommonJS, TypeScript, and monorepo configurations
  • API Reference for complete documentation of all functions and types
  • Error Codes to handle validation failures gracefully
MIT License GitHub · npm

Network data from NCC allocations. MNP may affect actual carriers.