郵便番号 REST API
APIドキュメント
REST APIサンプル
サンプル
新橋を含む郵便番号の一覧
$ curl -s 'https://postcode.teraren.com/postcodes.json?s=新橋&per=3' | jq
[
{
"jis": "01206",
"old": "085",
"new": "0850046",
"prefecture_kana": "ホッカイドウ",
"city_kana": "クシロシ",
"suburb_kana": "シンバシオオドオリ",
"prefecture": "北海道",
"city": "釧路市",
"suburb": "新橋大通",
"is_separated_suburb": 0,
"is_koaza": 0,
"is_chome": 1,
"is_include_area": 0,
"status": 0,
"reason": 0,
"url": "https://postcode.teraren.com/postcodes/0850046.json"
},
{
"jis": "04202",
"old": "986",
"new": "9860878",
"prefecture_kana": "ミヤギケン",
"city_kana": "イシノマキシ",
"suburb_kana": "シンバシ",
"prefecture": "宮城県",
"city": "石巻市",
"suburb": "新橋",
"is_separated_suburb": 0,
"is_koaza": 0,
"is_chome": 0,
"is_include_area": 0,
"status": 0,
"reason": 0,
"url": "https://postcode.teraren.com/postcodes/9860878.json"
},
{
"jis": "04213",
"old": "98956",
"new": "9895617",
"prefecture_kana": "ミヤギケン",
"city_kana": "クリハラシ",
"suburb_kana": "シワヒメシンハシモト",
"prefecture": "宮城県",
"city": "栗原市",
"suburb": "志波姫新橋本",
"is_separated_suburb": 0,
"is_koaza": 0,
"is_chome": 0,
"is_include_area": 0,
"status": 0,
"reason": 0,
"url": "https://postcode.teraren.com/postcodes/9895617.json"
}
]
$ curl -s https://postcode.teraren.com/postcodes/3230831.json | jq
{
"postcode_type": "area",
"jis": "09208",
"old": "323",
"new": "3230831",
"prefecture_kana": "トチギケン",
"city_kana": "オヤマシ",
"suburb_kana": "アマガヤチョウ",
"prefecture": "栃木県",
"city": "小山市",
"suburb": "雨ケ谷町",
"street_address": null,
"office": null,
"office_kana": null,
"post_type": null,
"is_separated_suburb": 0,
"is_koaza": 0,
"is_chome": 0,
"is_include_area": 0,
"status": 0,
"reason": 0,
"created_at": "2022-05-17T05:11:51.000Z",
"updated_at": "2022-05-17T05:11:51.000Z"
}
Webページに設置するサンプル
デモ
郵便番号を入力して、住所情報を自動取得する実装例
ライブデモ
サンプルコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.getElementById('postcode').addEventListener('keyup', function(e) { | |
if (!e.key.match(/[0-9]/)) return; | |
const postcode = e.target.value; | |
if (postcode.length !== 7) return; | |
const url = `https://postcode.teraren.com/postcodes/${postcode}.json`; | |
fetch(url) | |
.then(response => response.json()) | |
.then(json => { | |
document.getElementById('prefecture').value = json.prefecture; | |
document.getElementById('city').value = json.city; | |
document.getElementById('suburb').value = json.suburb; | |
}) | |
.catch(error => { | |
console.error(error); | |
['prefecture', 'city', 'suburb'].forEach(id => { | |
document.getElementById(id).value = ''; | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form> | |
<input type="text" id="postcode" placeholder="1600023" maxlength="7" autofocus> | |
<input type="text" id="prefecture" placeholder="東京都" disabled> | |
<input type="text" id="city" placeholder="新宿区" disabled> | |
<input type="text" id="suburb" placeholder="西新宿" disabled> | |
</form> |