# estat-api
> Unofficial async Rust client for the e-Stat API v3.0 (JSON), the API of Japan's portal site for official statistics. Searches statistical tables, reads their dimensions (metadata), downloads values with filters and paging or for several tables in one request, searches the catalog of statistical files, and registers and reads datasets (saved filters). Covers every function of API v3.0 (JSON; postDataset answers in XML). Requires an e-Stat application ID.
Crate name: `estat-api` (import as `estat_api`). MSRV 1.88, Tokio, reqwest.
Use it for: Japanese government statistics such as the census (国勢調査), population estimates (人口推計), labour force survey (労働力調査), economic census (経済センサス), household and price statistics, by prefecture or municipality and over time. Japanese terms: 統計表情報取得 = `stats_list` (getStatsList), メタ情報取得 = `meta_info` (getMetaInfo), 統計データ取得 = `stats_data` / `stats_data_all` (getStatsData), データカタログ情報取得 = `data_catalog` (getDataCatalog), 統計データ一括取得 = `stats_datas` (getStatsDatas), データセット登録 = `post_dataset` (postDataset), データセット参照 = `datasets` / `dataset` (refDataset), 統計表ID = `statsDataId` / `TableInf::id`, 政府統計コード = `stats_code`, アプリケーションID = `appId`, 表章事項 = `tab`, 時間軸事項 = `time`, 地域事項 = `area`, 分類事項 = `cat01`..`cat15`.
Public items:
- `EstatApi::{with_app_id, from_env (ESTAT_APP_ID), with_client, with_timeout, with_lang(Lang::{Japanese, English}), with_base_url}`
- `stats_list(&StatsListParams) -> Result<StatsList, EstatError>`; `StatsList { result, parameter, datalist_inf: DataListInf { number, result_inf, table_inf: Vec<TableInf>, list_inf: Vec<ListInf> } }`
- `meta_info(impl Into<MetaInfoParams>) -> Result<MetaInfo, _>` (a table ID string works); `MetaInfo { metadata_inf: MetadataInf { table_inf, class_inf: ClassInf { class_obj: Vec<ClassObj { id, name, description, explanation, class: Vec<Class { code, name, level, unit, parent_code, add_inf }> }> } } }`; `ClassInf::get(Dimension)`, `ClassObj::find(code)`
- `stats_data(&StatsDataParams) -> Result<StatsData, _>`, `stats_data_all(&StatsDataParams)` (follows NEXT_KEY); `StatsData { statistical_data: StatisticalData { result_inf: ResultInf { total_number, from_number, to_number, next_key }, table_inf, class_inf: Option<ClassInf>, data_inf: DataInf { note, annotation, value: Vec<DataValue> } } }`; `StatsData::{values(), next_key(), label(&DataValue, Dimension)}`
- `DataValue { tab, cat01..cat15, area, time, unit, annotation: Option<String>, value: String }` with `code(Dimension)` and `as_f64()` (`None` for `-`, `X` and other special characters)
- `data_catalog(&DataCatalogParams) -> Result<DataCatalog, _>`; `catalog::{DataCatalogListInf, DataCatalogInf { id, dataset: Dataset, resources: Resources { resource: Vec<Resource { id, title, url, format, .. }> } }}`
- `stats_datas(&StatsDatasParams) -> Result<StatsDatas, _>`: `StatsDatasParams::new().request(StatsDatasRequest::{table(id), dataset(id)}.code/level/code_from/code_to/start_position/limit/param(name, value))` plus `meta`, `explanation`, `annotation`, `replace_sp_char`, `spec()`; `StatsDatas { result, parameter_list, statistical_data_list: StatisticalDataList { result_inf, table_inf_list, class_inf_list, data_inf_list } }` with `table(n)`, `class_inf(n)`, `values(n)` by request number from 1; `ClassInf::label(&DataValue, Dimension)`. At most 100,000 values in total, no paging; status 2 = some requests matched nothing
- `datasets(&DatasetsParams) -> Result<DatasetList, _>`, `dataset(impl Into<DatasetParams>) -> Result<RefDataset, _>`; `dataset::DatasetInf { id, dataset_name, narrowing_cond: Option<Map>, public_state, result_inf, table_inf }`
- `post_dataset(&PostDatasetParams) -> Result<PostDataset { result, regist_inf: RegistInf { mode, dataset_id, stats_data_id, public_state, total_number } }, _>`: `PostDatasetParams::register(table_id).dataset_id(..).name(..).public(bool)` with the filter setters (same ID again = update), `PostDatasetParams::delete(dataset_id)`. Writes to e-Stat under the application ID
- `TableInf { id, stat_name: Option<CodeName { code, name }>, gov_org, statistics_name, title: Option<Title { no, name }>, cycle, survey_date, open_date, small_area, collect_area, main_category, sub_category, overall_total_number, updated_date, statistics_name_spec, description, title_spec }`
- Params builders: `StatsListParams::new().search_word(..).stats_code(..).stats_field(..).survey_years(..).open_years(..).updated_date(..).search_kind(SearchKind)/collect_area(CollectArea)/explanation(bool)/stats_name_list()/start_position(n)/limit(n)`; `StatsDataParams::{table(id), dataset(id)}.code(dim, codes)/code_from/code_to/level(dim, level)/start_position/limit/meta(bool)/count_only()/explanation/annotation/replace_sp_char(ReplaceSpChar)`; `DataCatalogParams::new()` with the search setters plus `data_types([DataType::{Xls, Csv, Pdf, Xml, XlsRep, Db}])`, `catalog_id`, `resource_id`
- `Dimension::{Tab, Time, Area, Cat01..Cat15}` with `id()`, `ALL`, `FromStr` ("area", "cat01", ...)
- `EstatError::{Connection, JsonParse, Api { status, message }, HttpStatus(u16, String), UnexpectedResponse(String), InvalidArgument(String)}` with `status()`, `is_unauthorized()` (100), `is_invalid_parameter()` (101-105), `is_server_error()` (200-299), `is_not_found()` (300), `is_timeout()`
- Every response struct has `extra: Map<String, Value>` with fields the crate does not know yet
CLI (feature `cli`, `cargo install estat-api --features cli`): `estat list [-q WORDS] [--stats-code C] [--stats-field F] [--survey-years Y] [--open-years Y] [--updated-date D] [--collect-area nationwide|prefecture|municipality] [--small-area] [--surveys] [--limit N] [--start N]`, `estat meta ID`, `estat data ID [--dataset] [--code DIM=CODES]... [--level DIM=L]... [--from DIM=C]... [--to DIM=C]... [--limit N] [--start N] [--all | --count-only]`, `estat catalog [search options] [--data-type xls|csv|pdf|xml|xls-rep|db]... [--catalog-id ID] [--resource-id ID]`, `estat datas --request "statsDataId=ID&cdArea=13000"...`, `estat datasets [--collect-area ..]`, `estat dataset ID`, `estat register-dataset TABLE_ID [--id ID] [--name NAME] [--public] [filters]`, `estat delete-dataset ID`. `data` and `datas` also take `--no-meta`, `--no-annotation`, `--replace-sp-char keep|zero|empty|na`. Global: `--app-id` (or `ESTAT_APP_ID`), `--lang ja|en`, `--ndjson`, `--timeout SECS` (default 300), `--no-explanation`. JSON on stdout (`{ok, command, params, rows|data, meta}`); `data` rows are flat (`area`, `area_name`, ..., `value`, `number`). Errors as JSON on stderr; exit codes 2 usage, 3 auth, 4 api, 5 not found, 1 other.
Common tasks:
- Find a table: `stats_list` with `search_word("人口 AND 都道府県")` or `stats_code` (8 digits for a survey), then use `TableInf::id`.
- Know the codes: `meta_info(id)`, then `class_inf.get(Dimension::Area)` and its `class` items (prefectures are `01000`..`47000`, whole country `00000`).
- Get values: `stats_data_all(&StatsDataParams::table(id).code(Dimension::Area, "13000"))`; check the size first with `count_only()` for large tables.
- Name the values: `data.label(value, Dimension::Time)`.
- Compare a few areas or tables at once: `stats_datas` with one `StatsDatasRequest` per area or table.
Minimal use (Tokio):
```rust
use estat_api::{Dimension, EstatApi, StatsDataParams, StatsListParams};
let api = EstatApi::from_env()?; // ESTAT_APP_ID
let list = api.stats_list(&StatsListParams::new().search_word("人口推計 AND 都道府県").limit(5)).await?;
let id = &list.datalist_inf.table_inf[0].id;
let data = api.stats_data_all(&StatsDataParams::table(id).code(Dimension::Area, "13000")).await?;
for v in data.values() {
println!("{:?} {} {:?}", data.label(v, Dimension::Time), v.value, v.as_f64());
}
```
Errors:
```rust
match api.meta_info("0003448237").await {
Err(e) if e.is_unauthorized() => {} // 100: wrong application ID
Err(e) if e.is_not_found() => {} // 300: no table or dataset with that ID
Err(e) if e.is_server_error() => {} // 200-299: retry later
Err(e) => eprintln!("{e}"),
Ok(meta) => {}
}
```
Limits and behaviour:
- `RESULT.STATUS`: 0 success, 1 success with nothing matched (empty lists, not an error), 2 bulk request where some requests matched nothing, 100+ errors (`EstatError::Api`): 100 application ID, 101-105 parameters, 200-299 server, 300 no such ID, 301/302 refused dataset change, 400 bulk over 100,000 values, 401 one bulk request failed.
- `getStatsData` returns up to 100,000 values per page (`limit`); `NEXT_KEY` (`StatsData::next_key()`) is the next `startPosition`. `stats_data_all` follows it; check the size first with `count_only()`.
- `getStatsDatas` has no paging and at most 100,000 values for all requests together.
- Default timeout 5 minutes, because some `getDataCatalog` searches take over a minute; change with `with_timeout`.
- Empty elements come as `""` and single elements as objects instead of arrays; the types handle both.
e-Stat returns errors in `RESULT.STATUS`; the crate turns 100 and above into `EstatError::Api`. Status 1 (no match) is a success with empty lists. No throttling or retries: the terms of use forbid many requests in a short time, and services using the API must show e-Stat's credit text.
## Documentation
- [README](https://github.com/uiuifree/rust-estat-api/blob/main/README.md): usage, paging, response types, errors, terms of use
- [API reference on docs.rs](https://docs.rs/estat-api)
- [CHANGELOG](https://github.com/uiuifree/rust-estat-api/blob/main/CHANGELOG.md)
- [examples/search.rs](https://github.com/uiuifree/rust-estat-api/blob/main/examples/search.rs): search tables and print values with names
- [examples/all_values.rs](https://github.com/uiuifree/rust-estat-api/blob/main/examples/all_values.rs): check the size of a table, then write all its values as CSV
- [examples/compare_areas.rs](https://github.com/uiuifree/rust-estat-api/blob/main/examples/compare_areas.rs): one bulk request with one request per prefecture
## Upstream
- [e-Stat API specification 3.0](https://www.e-stat.go.jp/api/api-info/e-stat-manual3-0)
- [e-Stat API usage guide](https://www.e-stat.go.jp/api/api-info/api-guide)
- [e-Stat API terms of use](https://www.e-stat.go.jp/api/agreement/)
- [Credit display](https://www.e-stat.go.jp/api/api-info/credit)