A lightweight and type-safe TypeScript library to fetch Polish company details by NIP (VAT identification number) from the official GUS (Statistics Poland) registry.
- Fetch company details using NIP (Polish VAT number)
- Type-safe API with TypeScript
- Built-in error handling
- Support for both production and test GUS environments
- No external dependencies beyond the Fetch API
npm install nipify
# or
yarn add nipify
# or
pnpm add nipify
import { getCompanyDetailsByNip } from 'nipify';
async function fetchCompanyInfo() {
try {
const { company } = await getCompanyDetailsByNip('1234567890', {
apiKey: 'YOUR_API_KEY',
});
console.log(company.companyName); // Company name
console.log(company.city); // City
console.log(company.street); // Street
// More fields available...
} catch (error) {
console.error('Error:', error.message);
}
}
Fetches company details from the GUS registry using a NIP number.
-
nip
(string): Polish VAT identification number (10 digits) -
options
(object): Configuration options-
apiKey
(string, required): Your GUS API key -
apiUrl
(string, optional): Custom API URL if needed -
testMode
(boolean, optional): Set totrue
to use the test environment. Default:false
-
corsProxy
(string, optional): URL of a CORS proxy for browser environments (see Browser Usage section)
-
Promise that resolves to an object with:
-
company
(object): Company details with the following properties:-
companyName
(string, optional): Name of the company -
city
(string, optional): City where the company is registered -
postalCode
(string, optional): Postal code -
street
(string, optional): Street name -
addressOne
(string, optional): Building number -
addressTwo
(string, optional): Apartment/office number
-
The function will throw an error if:
- NIP is not provided
- API key is not provided
- API authentication fails
- Company data cannot be retrieved
- Any network or parsing errors occur
The GUS API doesn't support Cross-Origin Resource Sharing (CORS), which means direct requests from browsers will fail with a CORS error like:
Access to fetch at 'https://wyszukiwarkaregon.stat.gov.pl/wsBIR/UslugaBIRzewnPubl.svc'
from origin 'http://localhost:3000' has been blocked by CORS policy
You can use a CORS proxy to relay requests to the GUS API:
const { company } = await getCompanyDetailsByNip('1234567890', {
apiKey: 'YOUR_API_KEY',
corsProxy: 'https://your-cors-proxy.com/',
});
Popular CORS proxy options:
- Set up your own proxy server
- Use a service like CORS Anywhere
- Create a serverless function (e.g., using AWS Lambda, Vercel, or Netlify functions)
For production applications, the recommended approach is to create a backend service that makes requests to the GUS API and then serves the data to your frontend:
// Your backend service
app.get('/api/company/:nip', async (req, res) => {
try {
const { company } = await getCompanyDetailsByNip(req.params.nip, {
apiKey: process.env.GUS_API_KEY,
});
res.json({ company });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Your frontend
fetch(`/api/company/1234567890`)
.then(response => response.json())
.then(data => console.log(data.company));
If you're only using this package in Node.js (not in browsers), you don't need to worry about CORS issues.
To use this package, you need an API key from the Polish GUS registry:
- Apply for API access at GUS API Registration
- Complete the registration process to receive your API key
- Use the key in your application as shown in the examples
GUS provides a test environment that doesn't count against your API quota:
const { company } = await getCompanyDetailsByNip('1234567890', {
apiKey: 'YOUR_API_KEY',
testMode: true, // Use test environment
});
If you need to specify a custom API URL:
const { company } = await getCompanyDetailsByNip('1234567890', {
apiKey: 'YOUR_API_KEY',
apiUrl: 'https://custom-api-url.example.com',
});
The library provides detailed error messages to help diagnose issues:
try {
const result = await getCompanyDetailsByNip('1234567890', {
apiKey: 'YOUR_API_KEY',
});
// Process result...
} catch (error) {
if (error.message.includes('NIP is required')) {
// Handle missing NIP
} else if (error.message.includes('API key is required')) {
// Handle missing API key
} else if (error.message.includes('Failed to login')) {
// Handle authentication failure
} else {
// Handle other errors
}
}
To build the package:
npm run build
This compiles TypeScript files to JavaScript in the dist
directory.
The package includes a comprehensive test suite:
# Run tests
npm test
# Run tests with coverage report
npm run test:coverage
MIT
Contributions are welcome! Feel free to open issues or submit pull requests.