{
  "openapi": "3.1.0",
  "info": {
    "title": "РСНМО — поиск по ИНН",
    "version": "1.2.0",
    "description": "Local, potentially incomplete company data. Authentication is not required in this release. Existing /api_inn.php is unchanged. GET may enqueue background enrichment."
  },
  "servers": [
    {
      "url": "https://www.rsnmo.ru"
    }
  ],
  "paths": {
    "/api/v1/company": {
      "get": {
        "operationId": "getCompanyByInn",
        "summary": "Сведения по ИНН",
        "parameters": [
          {
            "in": "query",
            "name": "inn",
            "required": true,
            "schema": {
              "type": "string",
              "pattern": "^(?:[0-9]{10}|[0-9]{12})$"
            },
            "description": "INN checksum must be valid; retain leading zeros.",
            "example": "7707329152"
          }
        ],
        "responses": {
          "200": {
            "description": "Local query succeeded; found=false is not proof of official absence.",
            "headers": {
              "X-Request-ID": {
                "schema": {
                  "type": "string"
                }
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Success"
                }
              }
            }
          },
          "400": {
            "description": "Invalid INN",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Failure"
                }
              }
            }
          },
          "405": {
            "description": "GET only",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Failure"
                }
              }
            }
          },
          "429": {
            "description": "Rate limited; Retry-After: 1",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Failure"
                }
              }
            }
          },
          "500": {
            "description": "Internal error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Failure"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/registry-history": {
      "get": {
        "operationId": "getRegistryHistory",
        "summary": "Полная загруженная история выбранного реестра по ИНН",
        "description": "Local records only. Ordered by record date descending then record_id. Each response uses one database snapshot; imports between requests can change pages. Distinct stored rows are retained even when document numbers coincide. CSV is fully generated before attachment headers are sent; no partial files on generation failure.",
        "parameters": [
          {
            "in": "query",
            "name": "inn",
            "required": true,
            "schema": {
              "type": "string",
              "pattern": "^(?:[0-9]{10}|[0-9]{12})$"
            },
            "description": "Valid INN checksum; preserve leading zeros."
          },
          {
            "in": "query",
            "name": "source",
            "required": false,
            "schema": {
              "type": "string",
              "default": "fsa-certificates-official"
            },
            "description": "Registry source_key from the portal selector. One source per request; financial and procurement sections are not supported."
          },
          {
            "in": "query",
            "name": "number",
            "required": false,
            "schema": {
              "type": "string",
              "maxLength": 200
            },
            "description": "Case-insensitive literal substring of displayed document number/details."
          },
          {
            "in": "query",
            "name": "status",
            "required": false,
            "schema": {
              "type": "string",
              "maxLength": 200
            },
            "description": "Case-insensitive substring of displayed status/details; Russian е and ё equivalent."
          },
          {
            "in": "query",
            "name": "date_from",
            "required": false,
            "schema": {
              "type": "string",
              "format": "date"
            },
            "description": "Inclusive record date, not download or check date. Unknown dates are excluded."
          },
          {
            "in": "query",
            "name": "date_to",
            "required": false,
            "schema": {
              "type": "string",
              "format": "date"
            },
            "description": "Inclusive record date."
          },
          {
            "in": "query",
            "name": "limit",
            "required": false,
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 100,
              "default": 30
            },
            "description": "Page size."
          },
          {
            "in": "query",
            "name": "offset",
            "required": false,
            "schema": {
              "type": "integer",
              "minimum": 0,
              "maximum": 10000000,
              "default": 0
            },
            "description": "Pass next_offset with unchanged filters."
          },
          {
            "in": "query",
            "name": "format",
            "schema": {
              "type": "string",
              "enum": [
                "json",
                "csv"
              ],
              "default": "json"
            },
            "description": "CSV exports all filtered rows from one snapshot; limit/offset are ignored. UTF-8 BOM, semicolon separator, header row. Formula-like and leading-zero numeric cells receive a leading apostrophe for spreadsheet safety."
          }
        ],
        "responses": {
          "200": {
            "description": "Page and exact filtered count. Empty local results do not prove official absence.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "api_version",
                    "request_id",
                    "data",
                    "error"
                  ],
                  "properties": {
                    "api_version": {
                      "const": "1"
                    },
                    "request_id": {
                      "type": "string"
                    },
                    "data": {
                      "type": "object",
                      "required": [
                        "items",
                        "total",
                        "returned",
                        "offset",
                        "limit",
                        "has_more",
                        "next_offset",
                        "freshness"
                      ],
                      "properties": {
                        "items": {
                          "type": "array",
                          "items": {
                            "type": "object",
                            "required": [
                              "record_id",
                              "source_key",
                              "source_title",
                              "title",
                              "number",
                              "status",
                              "date",
                              "date_iso",
                              "source_downloaded_at"
                            ],
                            "properties": {
                              "record_id": {
                                "type": "string"
                              },
                              "source_key": {
                                "type": "string"
                              },
                              "source_title": {
                                "type": "string"
                              },
                              "title": {
                                "type": "string"
                              },
                              "number": {
                                "type": "string"
                              },
                              "status": {
                                "type": "string"
                              },
                              "date": {
                                "type": "string"
                              },
                              "date_iso": {
                                "type": [
                                  "string",
                                  "null"
                                ]
                              },
                              "source_downloaded_at": {
                                "type": [
                                  "string",
                                  "null"
                                ]
                              },
                              "source_data_date": {
                                "type": [
                                  "string",
                                  "null"
                                ],
                                "format": "date"
                              },
                              "status_checked_at": {
                                "type": [
                                  "string",
                                  "null"
                                ],
                                "description": "Stored per-document successful check time; not a whole-source check."
                              }
                            }
                          }
                        },
                        "total": {
                          "type": "integer",
                          "minimum": 0
                        },
                        "returned": {
                          "type": "integer",
                          "minimum": 0
                        },
                        "offset": {
                          "type": "integer"
                        },
                        "limit": {
                          "type": "integer"
                        },
                        "has_more": {
                          "type": "boolean"
                        },
                        "next_offset": {
                          "type": [
                            "integer",
                            "null"
                          ]
                        },
                        "inn": {
                          "type": "string"
                        },
                        "source_key": {
                          "type": "string"
                        },
                        "source_title": {
                          "type": "string"
                        },
                        "coverage_note": {
                          "type": "string"
                        },
                        "freshness": {
                          "type": "object",
                          "description": "Dates across all filtered loaded records, not just this page. Unknown source snapshot dates remain null. Checked count refers only to successful per-document status verification and never asserts whole-registry freshness. Document dates and file download dates are not used as snapshot dates.",
                          "properties": {
                            "record_count": {
                              "type": "integer",
                              "minimum": 0
                            },
                            "data_date_known_records": {
                              "type": "integer",
                              "minimum": 0
                            },
                            "checked_records": {
                              "type": "integer",
                              "minimum": 0
                            },
                            "scope": {
                              "const": "filtered_loaded_records"
                            },
                            "mode": {
                              "type": "string",
                              "enum": [
                                "archive",
                                "loaded_records"
                              ]
                            },
                            "note": {
                              "type": "string"
                            },
                            "data_date_min": {
                              "type": [
                                "string",
                                "null"
                              ],
                              "format": "date"
                            },
                            "data_date_max": {
                              "type": [
                                "string",
                                "null"
                              ],
                              "format": "date"
                            },
                            "loaded_at_min": {
                              "type": [
                                "string",
                                "null"
                              ],
                              "format": "date-time"
                            },
                            "loaded_at_max": {
                              "type": [
                                "string",
                                "null"
                              ],
                              "format": "date-time"
                            },
                            "checked_at_min": {
                              "type": [
                                "string",
                                "null"
                              ],
                              "format": "date-time"
                            },
                            "checked_at_max": {
                              "type": [
                                "string",
                                "null"
                              ],
                              "format": "date-time"
                            }
                          },
                          "required": [
                            "record_count",
                            "data_date_known_records",
                            "checked_records",
                            "scope",
                            "mode",
                            "note",
                            "data_date_min",
                            "data_date_max",
                            "loaded_at_min",
                            "loaded_at_max",
                            "checked_at_min",
                            "checked_at_max"
                          ]
                        }
                      }
                    },
                    "meta": {
                      "type": "object"
                    },
                    "error": {
                      "type": "null"
                    }
                  }
                }
              },
              "text/csv": {
                "schema": {
                  "type": "string"
                },
                "example": "ИНН;Источник;Код источника;Запись;Номер;Статус / сведения;Дата записи;Дата загрузки;Идентификатор строки\r\n"
              }
            },
            "headers": {
              "X-Total-Count": {
                "description": "Number of data rows in CSV; absent for JSON.",
                "schema": {
                  "type": "integer",
                  "minimum": 0
                }
              }
            }
          },
          "400": {
            "description": "Invalid INN, source, filters or pagination"
          },
          "405": {
            "description": "Only GET supported"
          },
          "429": {
            "description": "Rate limited; honor Retry-After"
          },
          "503": {
            "description": "Source unavailable; retry later"
          },
          "422": {
            "description": "export_too_large: more than 64 MiB or generation budget exceeded. No partial CSV. Narrow filters."
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Source": {
        "type": "object",
        "required": [
          "source_key",
          "title",
          "lookup_status",
          "returned_records",
          "data_loaded_at",
          "last_successful_import_at",
          "update_status",
          "freshness_threshold_days",
          "coverage_scope"
        ],
        "properties": {
          "source_key": {
            "type": "string"
          },
          "title": {
            "type": "string"
          },
          "lookup_status": {
            "type": "string",
            "enum": [
              "found",
              "no_local_data",
              "not_checked",
              "unavailable"
            ]
          },
          "returned_records": {
            "type": "integer",
            "minimum": 0
          },
          "data_loaded_at": {
            "type": [
              "string",
              "null"
            ],
            "format": "date-time"
          },
          "last_successful_import_at": {
            "type": [
              "string",
              "null"
            ],
            "format": "date-time"
          },
          "update_status": {
            "type": "string",
            "enum": [
              "fresh",
              "stale",
              "unavailable",
              "unknown"
            ]
          },
          "freshness_threshold_days": {
            "type": [
              "integer",
              "null"
            ]
          },
          "coverage_scope": {
            "type": "string",
            "enum": [
              "per_inn",
              "not_asserted"
            ]
          }
        }
      },
      "Success": {
        "type": "object",
        "required": [
          "api_version",
          "request_id",
          "generated_at",
          "data",
          "meta",
          "error"
        ],
        "properties": {
          "api_version": {
            "const": "1"
          },
          "request_id": {
            "type": "string"
          },
          "generated_at": {
            "type": "string",
            "format": "date-time"
          },
          "error": {
            "type": "null"
          },
          "data": {
            "type": "object",
            "required": [
              "inn",
              "found",
              "identity",
              "finance",
              "procurement",
              "registries"
            ],
            "properties": {
              "inn": {
                "type": "string"
              },
              "found": {
                "type": "boolean"
              },
              "identity": {
                "type": "object",
                "additionalProperties": true
              },
              "finance": {
                "type": "object",
                "additionalProperties": true
              },
              "procurement": {
                "type": "object",
                "additionalProperties": true
              },
              "registries": {
                "type": "object",
                "required": [
                  "items",
                  "groups"
                ],
                "properties": {
                  "items": {
                    "type": "array",
                    "items": {
                      "type": "object",
                      "additionalProperties": true
                    }
                  },
                  "groups": {
                    "type": [
                      "object",
                      "array"
                    ]
                  }
                }
              }
            },
            "additionalProperties": true
          },
          "meta": {
            "type": "object",
            "required": [
              "completeness",
              "scope",
              "monitor_generated_at",
              "monitor_status",
              "limitations",
              "list_limits",
              "sources"
            ],
            "properties": {
              "completeness": {
                "const": "partial"
              },
              "scope": {
                "const": "loaded_local_data"
              },
              "monitor_generated_at": {
                "type": [
                  "string",
                  "null"
                ],
                "format": "date-time"
              },
              "monitor_status": {
                "type": "string",
                "enum": [
                  "current",
                  "unknown"
                ]
              },
              "limitations": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "list_limits": {
                "type": "object",
                "additionalProperties": {
                  "type": "integer"
                }
              },
              "sources": {
                "type": "array",
                "items": {
                  "$ref": "#/components/schemas/Source"
                }
              }
            }
          }
        }
      },
      "Failure": {
        "type": "object",
        "required": [
          "api_version",
          "request_id",
          "data",
          "error"
        ],
        "properties": {
          "api_version": {
            "const": "1"
          },
          "request_id": {
            "type": "string"
          },
          "data": {
            "type": "null"
          },
          "error": {
            "type": "object",
            "required": [
              "code",
              "message"
            ],
            "properties": {
              "code": {
                "type": "string"
              },
              "message": {
                "type": "string"
              }
            }
          }
        }
      }
    }
  }
}
