Authentication
The PostNL API requires an API key to authenticate. You can request one via MijnPostNL.
If you do not already have a PostNL account, be sure to check out this page: https://developer.postnl.nl/request-api-key/
Note
The API key is automatically attached to the Firstred\PostNL\Entity\SOAP\UsernameToken object (SOAP) or apikey HTTP header (REST). You do not have to manually add the API key with every request.
Passing all credentials
Besides having to provide an API key, you will have to pass information about your business. These credentials will have to be passed with a Firstred\PostNL\Entity\Customer object when creating a new Firstred\PostNL\PostNL instance.
Note
The Firstred\PostNL\PostNL class is the main class of this library. It handles all functionality you will need from a developer’s perspective. After instantiating a new Firstred\PostNL\PostNL object you will have everything you need to communicate with the PostNL API. Everything else (caching, HTTP Clients, logging, etc.) is optional.
In order to get started with the API, the following credentials are important:
Required credentials
- API key
- Required:
TrueThe API key
- Customer code
- Required:
TrueThe customer code is a code that usually consists of 4 letters and appears in domestic 3S-codes.
- Customer number
- Required:
TrueThe customer number is a number that usually consists of 8 digits.
- Address
A filled
Firstred\PostNL\Entity\Addressobject with at least the following information:- AddressType
- Required:
TrueDefault:02The address type should be
02, which means the address belongs to the sender.
- City
- Required:
TrueCity
- CompanyName
- Required:
TrueThe company name
- HouseNr
- Required:
TrueThe house number
- Street
- Required:
TrueStreet name
- Zipcode
- Required:
TrueThe postcode. Be aware that the API might sometimes refer to a postcode as postcode, postal code or zipcode.
- Collection location
- Required:
TrueDefault:123456I must admit that to this day I still do not have a single clue what this value means. It could refer to your local drop-off location (if you use one). If your PostNL account manager can provide you with a collection location number please use that one.
I usually fill out
123456and it seems to work just fine.
- Globalpack barcode type
- Required:
FalseThe barcode type to use for international shipments. This field is optional if you do not ship outside the EU.
This field usually consists of 2 letters.
- Globalpack customer code
- Required:
FalseThe barcode type to use for international shipments. This field is optional if you do not ship outside the EU.
This field usually consists of 4 digits.
When you have all the required information, you are ready to configure the library. It can be configured as follows:
Note
Example configuration. All the credential come together in the Firstred\PostNL\Entity\Customer and main Firstred\PostNL\PostNL class.
<?php
use Firstred\PostNL\Entity\Address;
use Firstred\PostNL\Entity\Customer;
$apiKey = 'qjsdjufhjasudhfaSDFasdifh324';
$customer = new Customer(
CustomerNumber: '11223344',
CustomerCode: 'DEVC',
CollectionLocation: '123456',
ContactPerson: 'Test',
Address: new Address(
AddressType: '02',
CompanyName: 'PostNL',
Street: 'Siriusdreef',
HouseNr: '42',
Zipcode: '2132WT',
City: 'Hoofddorp',
Countrycode: 'NL',
),
GlobalPackCustomerCode: '1234',
GlobalPackBarcodeType: 'AB',
);
$postnl = new PostNL(
customer: $customer, // The filled Customer object
apiKey: $apiKey, // The API key
sandbox: false, // Sandbox = false, meaning we are now using the live environment
);
You might have noticed that several different ways have been used to instantiate an object. More information about this can be found in the Object instantiation section.
The PostNL client constructor accepts a few options:
- customer
- Required:
TrueThe
Firstred\PostNL\Entity\Customerobject that is used to configure the client and let PostNL know who is requesting the data.<?php use Firstred\PostNL\Entity\Address; use Firstred\PostNL\Entity\Customer; // Create a new customer $client = new Customer( CustomerNumber: '11223344', // Your collection location CustomerCode: 'DEVC', // Your Customer Code CollectionLocation: '123456', // Your Customer Number ContactPerson: 'Sander', // Add your GlobalPack information if you need Email: 'test@voorbeeld.nl', // to create international shipment labels Name: 'Michael', Address: new Address( AddressType: '02', // This address will be shown on the label CompanyName: 'PostNL', Street: 'Siriusdreef', HouseNr: '42', Zipcode: '2132WT', City: 'Hoofddorp', Countrycode: 'NL', ), GlobalPackCustomerCode: '1234', GlobalPackBarcodeType: 'CX', );
- apiKey
The API key to use. Note that if you want to switch from the legacy API to the new SOAP and REST API you will have to request a new key.
If you want to connect to the legacy API, you should pass a
Firstred\PostNL\Entity\SOAP\UsernameTokenwith your username and token set:$usernameToken = new UsernameToken('username', 'token');
You can request an API key for the sandbox environment on this page: https://developer.postnl.nl/content/request-api-key For a live key you should contact your PostNL account manager.
- sandbox
- Required:
TrueIndicate whether you’d like to connect to the sandbox environment. When false the library uses the live endpoints.