メインコンテンツまでスキップ
バージョン: 🚧 Canary

📮 Address Service

The Address Service (addressService) exposes authenticated address lookup by postal code and delegates the lookup to an injected findAddressDriver. There is no built-in default driver — you must inject one (for example via createJapanPostDriver(...)) or lookups fail with the block's wrapped service error. The route schema accepts partial postal codes, but the bundled Japan Post driver ultimately requires a normalized 7-digit postal code after hyphens are removed. An optional in-memory cache can also be configured.


🚀 Quickstart

import express from 'express';
import {middlewares, services, drivers, utils} from '@nodeblocks/backend-sdk';

const {nodeBlocksErrorMiddleware} = middlewares;
const {addressService} = services;
const {withMongo, createJapanPostDriver} = drivers;
const {createCache} = utils;

const connectToDatabase = withMongo('mongodb://localhost:27017/?authSource=admin', 'dev', 'user', 'password');

const findAddressDriver = createJapanPostDriver(
process.env.JAPAN_POST_CLIENT_ID!,
process.env.JAPAN_POST_SECRET_KEY!,
// optional third arg: hostname, default 'api.da.pf.japanpost.jp'
);

express()
.use(
addressService(
{
...(await connectToDatabase('identities')),
},
{
authSecrets: {
authEncSecret: 'your-encryption-secret',
authSignSecret: 'your-signing-secret',
},
authMode: 'bearer', // or 'cookie'
findAddressCache: createCache(),
},
{findAddressDriver},
),
)
.use(nodeBlocksErrorMiddleware())
.listen(8089, () => console.log('Server running'));

🍪 Cookie auth: When authMode: 'cookie', protected routes read the access token from cookies. Host apps must register cookie-parser.


📋 Endpoint Summary

MethodPathDescriptionAuthorization
GET/addresses?postalCode=...Look up an address by Japanese postal codeAuthenticated

🗄️ Response Shape

Successful lookups return:

{
prefecture: string;
city: string;
town?: string;
postalCode: string;
}
FieldTypeDescription
prefecturestringPrefecture name
citystringCity / municipality name
townstringOptional town name
postalCodestringNormalized postal code

🔐 Authentication Headers

Authorization: Bearer <access_token>
x-nb-fingerprint: <device_fingerprint>

The x-nb-fingerprint header is required for authenticated requests when fingerprint was specified during login.


🔧 API Endpoints

1. Find Address

Request:

  • Method: GET
  • Path: /addresses
  • Authorization: Authenticated

Query Parameters:

FieldTypeRequiredDescription
postalCodestringPartial or full Japanese postal code (^\d{3}-?\d{0,4}$, e.g. 100 or 100-0001). If you use createJapanPostDriver, the normalized value must be 7 digits.

Response: The route returns the address object; the service sends it with Express's default JSON status (200).

Example:

curl "{{host}}/addresses?postalCode=100-0001" \
-H "Authorization: Bearer <access-token>"

Common errors:

StatusDescription
400Validation error (missing/invalid postalCode)
401Missing or invalid auth
404Address not found
500Address lookup driver failure

⚙️ Configuration Options

interface AddressServiceConfiguration {
authSecrets: {
authEncSecret: string;
authSignSecret: string;
};
authMode?: 'bearer' | 'cookie';
identity?: {
typeIds?: {
admin: string;
guest: string;
regular: string;
};
};
findAddressCache?: ReturnType<typeof createCache>;
}

Datastore

CollectionRequiredDescription
identitiesRequired by the service type, but not used by the address route itself

Options (third argument)

OptionRequiredDescription
findAddressDriver✅ for lookupsAddress lookup driver ({ findAddress(postalCode): Promise<Address> }). Use createJapanPostDriver for Japan Post. Without it, find-address requests fail.

🚨 Error Handling

StatusError MessageDescription
400Validation ErrorMissing or invalid postalCode query parameter
401Authentication failureThe auth layer rejected the request
404No matching address found.The lookup returned no address
500Failed to find address.The address lookup driver failed