estat-api
Unofficial async Rust client and command line tool for the e-Stat API v3.0, the API of e-Stat, the portal site for official statistics of Japan. It searches statistical tables, reads their dimensions, downloads the values (one table at a time or several in one request), searches the catalog of statistical files and manages datasets.
- Census (国勢調査), population estimates, labour force survey, economic census and the other surveys in the e-Stat database
- Filters by area, time, tabulation item and classification, with paging done for you
- Typed responses that handle e-Stat's JSON quirks: a single element comes as an object instead of an array, and numeric text comes as a JSON number
estatCLI with JSON output, for scripts and AI agents
This crate is not made or endorsed by the Statistics Bureau of Japan or the National Statistics Center. A summary for AI coding assistants is in llms.txt.
| e-Stat API | Method | CLI |
|---|---|---|
getStatsList (統計表情報取得) |
stats_list |
estat list |
getMetaInfo (メタ情報取得) |
meta_info |
estat meta |
getStatsData (統計データ取得) |
stats_data, stats_data_all |
estat data |
getStatsDatas (統計データ一括取得) |
stats_datas |
estat datas |
getDataCatalog (データカタログ情報取得) |
data_catalog |
estat catalog |
refDataset (データセット参照) |
datasets, dataset |
estat datasets, estat dataset |
postDataset (データセット登録) |
post_dataset |
estat register-dataset, estat delete-dataset |
日本語での概要
政府統計の総合窓口(e-Stat)の API(Version 3.0、JSON 形式)を Rust から使うための非公式クライアントと
CLI です。統計表情報取得(getStatsList)、メタ情報取得(getMetaInfo)、統計データ取得
(getStatsData)、統計データ一括取得(getStatsDatas)、データカタログ情報取得(getDataCatalog)、
データセット登録(postDataset)とデータセット参照(refDataset)の API v3.0 の機能すべてに対応しています。統計データは
NEXT_KEY をたどって全件を取得するヘルパーもあります。e-Stat の JSON は要素が 1 つだと配列ではなく
オブジェクトになるので、どちらで来ても Vec として受け取れるようにしています。アプリケーション ID は
同梱していないので、e-Stat で発行したものを渡してください。
アプリケーション ID(appId)は、e-Stat にユーザー登録し、マイページの「API機能(アプリケーションID発行)」で
発行します。環境変数 ESTAT_APP_ID に入れておけば EstatApi::from_env() で読み込めます。
統計表 ID(statsDataId)を指定して、東京都(地域コード 13000)の値を取り出す例です。
use ;
# async
コマンドラインでは次のように使えます。
Setup
Register on e-Stat and issue an application ID from "API機能(アプリケーションID発行)" on My Page, as
described in the usage guide. The ID is not
bundled with this crate; pass your own, or set ESTAT_APP_ID.
[]
= "0.1"
= { = "1", = ["macros", "rt-multi-thread"] }
Usage
use ;
async
Requests time out after 5 minutes, since some catalog searches take more than a minute on the
e-Stat side. Change it with with_timeout, or pass your own
reqwest::Client with with_client. with_lang(Lang::English) asks for English names where the
table has them.
Tables and metadata
stats_list searches tables by keyword (AND, OR and NOT work), government statistics code,
statistics field, survey or release period, and update date. Each TableInf has the table ID to
pass to the other calls. stats_name_list() lists surveys instead of tables.
meta_info(id) returns the dimensions of a table: the tabulation item (tab), time (time), area
(area) and classifications (cat01 to cat15), each with its items and their codes, names,
levels and parent codes. ClassInf::get(Dimension::Area) picks one dimension and
ClassObj::find(code) one item.
Values and paging
stats_data returns one page of values for a table (StatsDataParams::table) or a dataset
registered on e-Stat (StatsDataParams::dataset). Narrow it down per dimension:
| Method | Parameter | Example |
|---|---|---|
code(dim, codes) |
cdArea, cdTime, ... |
code(Dimension::Area, "13000,14000") |
code_from(dim, code) |
cdAreaFrom, ... |
code_from(Dimension::Time, "2015000000") |
code_to(dim, code) |
cdAreaTo, ... |
code_to(Dimension::Time, "max") |
level(dim, level) |
lvArea, ... |
level(Dimension::Area, "2") |
A page holds up to 100,000 values (limit). When there are more, StatsData::next_key() is the
startPosition of the next page. stats_data_all follows it to the end and returns all values in
one StatsData; later pages are requested without metadata. count_only() returns just the number
of values, which is a cheap way to check the size first.
Values are strings, because e-Stat puts characters such as - or X in place of numbers;
DataValue::as_f64() gives the number and DataInf::note explains the characters.
StatsData::label(value, dim) looks up the item name of a value in the metadata.
Several tables in one request
stats_datas sends getStatsDatas: one POST with several requests, each a table or dataset with
its own filters. The requests together may return at most 100,000 values, and there is no paging.
The response refers to each request by its number, from 1 in the order added.
use ;
# async
Status 2 means some requests matched nothing; the message lists their numbers. Status 400 (too
many values) and 401 (one request failed, with its error in the message) are errors.
Datasets
A dataset stores a table ID and filters under your application ID. post_dataset registers,
updates or deletes one, datasets lists the datasets you can use (yours and those others made
public), dataset(id) returns one with its filters, and StatsDataParams::dataset(id) reads its
values.
use ;
# async
postDataset changes data on e-Stat. e-Stat answers it in XML only, so the crate reads the few
elements of that response itself, without an XML dependency.
Data catalog
data_catalog searches the files (Excel, CSV, PDF, XML) and database tables on e-Stat. Each
DataCatalogInf has the dataset and its resources; a resource's url is the download URL, or the
table ID for database tables (format DB).
Response types
Field names follow the e-Stat tags in snake case (TABLE_INF is table_inf, @parentCode is
parent_code), so the API specification
applies as it is. e-Stat converts XML to JSON, which has some side effects the types deal with:
- An element that can repeat is an object when there is one and an array when there are more.
Here it is always a
Vec. - Text that looks like a number becomes a JSON number (
"SURVEY_DATE": 201606). Text fields accept both and keep a string; counts accept both and keep a number. - An empty element becomes
"":DESCRIPTIONis thenNone, andRESULT_INF(no match) orDATA_INF(count_only) an empty struct. TITLEis a plain string when the table has no number, and an object with@nowhen it has.
Fields this crate does not know are kept in extra on every struct, and serializing a struct gives
them back.
Errors
e-Stat reports errors in RESULT.STATUS of the body. The crate checks it and returns
EstatError::Api { status, message } for status 100 and above. Status 1 (nothing matched) is
not an error: the call succeeds with an empty list.
use EstatApi;
# async
| Status | Meaning |
|---|---|
0 |
success |
1 |
success, nothing matched |
100 |
wrong application ID |
101 to 105 |
missing or invalid parameter |
200 to 299 |
server error |
300 |
no table or dataset with that ID |
301 |
the dataset belongs to another application ID |
302 |
the dataset's filters select nothing |
400 |
bulk request selects more than 100,000 values |
401 |
one request of a bulk request failed |
The application ID is sent in the URL. The crate removes the URL from connection errors and hides
the ID in Debug output.
Command line tool
The cli feature adds an estat command. It reads the application ID from ESTAT_APP_ID (or
--app-id) and prints JSON to stdout.
data prints one row per value with the codes, the item names (area_name, ...), value as sent
and number (null for special characters). Filters are --code DIM=CODES, --level DIM=LEVEL,
--from DIM=CODE and --to DIM=CODE, where DIM is tab, time, area or cat01 to cat15.
--all follows NEXT_KEY; without it, meta.next_key is the value to pass to --start.
--count-only prints just meta.total, to check the size before --all. --no-meta,
--no-annotation and --replace-sp-char keep|zero|empty|na set the other options of the values.
datas takes each request as e-Stat parameters in query string form and prints the values of all
requests as rows with request_no. register-dataset takes the same filters as data, plus
--id, --name and --public.
On every command, --no-explanation leaves out explanations, --ndjson prints one row per line,
and --timeout SECS changes the 5 minute timeout.
Results are {"ok": true, "command", "params", "rows" or "data", "meta"}, where params is the
PARAMETER element e-Stat echoes back. Errors are printed to stderr as {"ok": false, "error": {"kind", "message", "status"}}.
| Exit code | Meaning |
|---|---|
0 |
success |
1 |
other failure, such as stdout that cannot be written |
2 |
invalid arguments or parameters |
3 |
missing or wrong application ID |
4 |
API or network error |
5 |
no table or dataset with that ID |
Terms of use
Follow the e-Stat API terms of use. In particular:
- Do not send many requests in a short time. This crate does not throttle or retry, and
stats_data_allsends one request per page, so narrow large tables down first. - A service built on the API must show this credit where its users can see it (details): 「このサービスは、政府統計総合窓口(e-Stat)のAPI機能を使用していますが、サービスの内容は国によって保証されたものではありません。」
- The statistics themselves are under the e-Stat terms of use.
Development
ESTAT_APP_ID=...
ESTAT_APP_ID=...
ESTAT_APP_ID=...
ESTAT_APP_ID=...
See CONTRIBUTING.md.
License
MIT