Vendure Address Lookup
This Vendure plugin allows you to lookup addresses based on postalcode, housenumber and/or streetname.
Getting started
import { AddressLookupPlugin } from '@pinelab/vendure-plugin-address-lookup';
plugins: [
AddressLookupPlugin.init({
lookupStrategies: [
new PostNLLookupStrategy({
apiKey: process.env.POSTNL_APIKEY!,
}),
new GooglePlacesLookupStrategy({
supportedCountryCodes: ['DE'],
apiKey: process.env.GOOGLE_PLACES_APIKEY!,
})
],
}),
DefaultSearchPlugin,
AdminUiPlugin.init({
port: 3002,
route: 'admin',
}),
],
Storefront usage
In your storefront, you can use the lookupAddress mutation to look up an address.
You need an active order to be able to access this query!
query {
lookupAddress(
input: { countryCode: "NL", postalCode: "8911 DM", houseNumber: "3" }
) {
streetLine1
streetLine2
postalCode
city
province
country
countryCode
}
}
query {
lookupAddress(
input: { countryCode: "BE", postalCode: "9052", houseNumber: "110" }
) {
streetLine1
streetLine2
postalCode
city
country
countryCode
}
}
query {
lookupAddress(
input: {
countryCode: "BE"
postalCode: "9052"
houseNumber: "110"
streetName: "Rijvisschepark"
}
) {
streetLine1
streetLine2
postalCode
city
country
countryCode
}
}
Google Places Strategy
If you want to use the Google Places API, you can do so by registering the GooglePlacesLookupStrategy in your config:
import { GooglePlacesLookupStrategy } from '@pinelab/vendure-plugin-address-lookup';
plugins: [
AddressLookupPlugin.init({
lookupStrategies: [
new GooglePlacesLookupStrategy({
supportedCountryCodes: ['DE'],
apiKey: process.env.GOOGLE_PLACES_APIKEY!,
}),
],
}),
],
The Google places API requires you to input the housenumber and streetname. It will mostly give you a single result, even when multiple results can exists.
If that happens, you can pass the postalcode to get a specific result.
{
lookupAddress(
input: { countryCode: "DE", streetName: "ParkstraĂźe", houseNumber: "4" }
) {
streetLine1
streetLine2
city
province
postalCode
country
countryCode
}
}
{
lookupAddress(
input: {
countryCode: "DE"
streetName: "ParkstraĂźe"
houseNumber: "4"
postalCode: "49624"
}
) {
streetLine1
streetLine2
city
province
postalCode
country
countryCode
}
}
Custom lookup strategies
If you want to implement your own strategy, for example to support more countries, or use different API's, you can do so by implementing the LookupStrategy interface:
import { AddressLookupStrategy } from '@pinelab/vendure-plugin-address-lookup';
export class GermanPostcodeStrategy implements AddressLookupStrategy {
readonly supportedCountryCodes = ['DE'];
constructor(private readonly input: PostcodeTechStrategyInput) {}
validateInput?(input: AddressLookupInput): true | string {
}
async lookup(
ctx: RequestContext,
input: AddressLookupInput
): Promise<OrderAddress[]> {
}
}