{
  "openapi": "3.1.0",
  "info": {
    "title": "API Greffe-Entreprises",
    "version": "1.0.0",
    "summary": "Annuaire public et archives d'entreprises françaises",
    "description": "API de consultation des entreprises françaises référencées dans les archives Greffe-Entreprises. Lecture unitaire sans clé ; export en volume avec clé partenaire. Données sources sous Licence Ouverte 2.0 (Etalab).",
    "contact": {
      "name": "Greffe-Entreprises — Partenariats",
      "email": "partenariats@greffe-entreprises.fr",
      "url": "https://www.greffe-entreprises.fr/partenariats"
    },
    "license": {
      "name": "Licence Ouverte 2.0 (Etalab)",
      "url": "https://www.etalab.gouv.fr/licence-ouverte-open-licence/"
    }
  },
  "servers": [
    {
      "url": "https://www.greffe-entreprises.fr",
      "description": "Production"
    }
  ],
  "tags": [
    { "name": "Entreprises", "description": "Recherche et fiches" },
    { "name": "Métadonnées", "description": "Découverte, licence, quotas" },
    { "name": "Partenaires", "description": "Export et flux en volume" },
    { "name": "Agents", "description": "MCP" }
  ],
  "paths": {
    "/api/v1/companies": {
      "get": {
        "tags": ["Entreprises"],
        "operationId": "searchCompanies",
        "summary": "Rechercher et lister les entreprises",
        "parameters": [
          { "name": "q", "in": "query", "description": "Dénomination, SIREN, SIRET, ville ou alias.", "schema": { "type": "string", "maxLength": 120 } },
          { "name": "limit", "in": "query", "schema": { "type": "integer", "minimum": 1, "maximum": 100, "default": 25 } },
          { "name": "offset", "in": "query", "schema": { "type": "integer", "minimum": 0, "maximum": 100000, "default": 0 } },
          { "name": "pretty", "in": "query", "schema": { "type": "boolean", "default": false } }
        ],
        "responses": {
          "200": {
            "description": "Liste paginée",
            "headers": {
              "ETag": { "schema": { "type": "string" } },
              "Link": { "schema": { "type": "string" } },
              "X-RateLimit-Limit": { "schema": { "type": "integer" } },
              "X-RateLimit-Remaining": { "schema": { "type": "integer" } }
            },
            "content": {
              "application/json": { "schema": { "$ref": "#/components/schemas/ListResponse" } }
            }
          },
          "304": { "description": "Non modifié" },
          "429": { "$ref": "#/components/responses/TooManyRequests" },
          "503": { "$ref": "#/components/responses/ServiceUnavailable" }
        }
      }
    },
    "/api/v1/companies/{siren}": {
      "get": {
        "tags": ["Entreprises"],
        "operationId": "getCompany",
        "summary": "Fiche complète d'une entreprise",
        "parameters": [
          { "name": "siren", "in": "path", "required": true, "description": "SIREN à 9 chiffres", "schema": { "type": "string", "pattern": "^[0-9]{9}$" } }
        ],
        "responses": {
          "200": {
            "description": "Fiche complète",
            "headers": {
              "ETag": { "schema": { "type": "string" } },
              "Link": { "schema": { "type": "string" } }
            },
            "content": {
              "application/json": { "schema": { "$ref": "#/components/schemas/CompanyResponse" } },
              "application/ld+json": { "schema": { "$ref": "#/components/schemas/CompanyResponse" } }
            }
          },
          "400": { "$ref": "#/components/responses/BadRequest" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "429": { "$ref": "#/components/responses/TooManyRequests" },
          "503": { "$ref": "#/components/responses/ServiceUnavailable" }
        }
      }
    },
    "/api/v1/meta": {
      "get": {
        "tags": ["Métadonnées"],
        "operationId": "getApiMeta",
        "summary": "Volumes, licence, quotas et endpoints",
        "responses": {
          "200": {
            "description": "Métadonnées",
            "content": { "application/json": { "schema": { "type": "object", "additionalProperties": true } } }
          },
          "429": { "$ref": "#/components/responses/TooManyRequests" }
        }
      }
    },
    "/api/v1/partners/manifest": {
      "get": {
        "tags": ["Partenaires"],
        "operationId": "getPartnerManifest",
        "summary": "Manifeste public d'échange de données",
        "description": "Schémas natif/D&B/格兰德, modes de livraison, licence, contact. Aucune donnée d'entreprise.",
        "responses": {
          "200": {
            "description": "Manifeste",
            "content": { "application/json": { "schema": { "type": "object", "additionalProperties": true } } }
          }
        }
      }
    },
    "/api/v1/partners/feed": {
      "get": {
        "tags": ["Partenaires"],
        "operationId": "getPartnerFeed",
        "summary": "Flux partenaire paginé",
        "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
        "parameters": [
          { "name": "profile", "in": "query", "schema": { "type": "string", "enum": ["native", "dnb", "grand"], "default": "native" } },
          { "name": "format", "in": "query", "schema": { "type": "string", "enum": ["ndjson", "json", "csv"], "default": "ndjson" } },
          { "name": "limit", "in": "query", "schema": { "type": "integer", "minimum": 1, "maximum": 2000, "default": 500 } },
          { "name": "offset", "in": "query", "schema": { "type": "integer", "minimum": 0, "default": 0 } },
          { "name": "since", "in": "query", "description": "Date ISO 8601 ; filtre sur la date d'immatriculation (et non sur une date de mise à jour).", "schema": { "type": "string", "format": "date" } }
        ],
        "responses": {
          "200": {
            "description": "Lot d'enregistrements",
            "headers": {
              "X-Dataset-Version": { "schema": { "type": "string" } },
              "X-Total-Count": { "schema": { "type": "integer" } },
              "X-Data-License": { "schema": { "type": "string" } }
            },
            "content": {
              "application/x-ndjson": { "schema": { "type": "string" } },
              "application/json": { "schema": { "type": "object", "additionalProperties": true } },
              "text/csv": { "schema": { "type": "string" } }
            }
          },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "429": { "$ref": "#/components/responses/TooManyRequests" }
        }
      }
    },
    "/api/v1/export/companies": {
      "get": {
        "tags": ["Partenaires"],
        "operationId": "exportCompanies",
        "summary": "Instantané complet versionné",
        "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
        "parameters": [
          { "name": "profile", "in": "query", "schema": { "type": "string", "enum": ["native", "dnb", "grand"], "default": "native" } },
          { "name": "format", "in": "query", "schema": { "type": "string", "enum": ["ndjson", "json", "csv"], "default": "ndjson" } },
          { "name": "limit", "in": "query", "schema": { "type": "integer", "minimum": 1, "maximum": 2000, "default": 2000 } },
          { "name": "offset", "in": "query", "schema": { "type": "integer", "minimum": 0, "default": 0 } }
        ],
        "responses": {
          "200": { "description": "Instantané complet" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "429": { "$ref": "#/components/responses/TooManyRequests" }
        }
      }
    },
    "/api/mcp": {
      "post": {
        "tags": ["Agents"],
        "operationId": "mcpJsonRpc",
        "summary": "Serveur MCP JSON-RPC 2.0",
        "description": "Méthodes initialize, tools/list, tools/call, ping. Outils : search_companies, get_company, list_company_archives, get_dataset_info.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["jsonrpc", "method"],
                "properties": {
                  "jsonrpc": { "type": "string", "const": "2.0" },
                  "id": { "oneOf": [{ "type": "string" }, { "type": "integer" }, { "type": "null" }] },
                  "method": { "type": "string" },
                  "params": { "type": "object", "additionalProperties": true }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Réponse JSON-RPC",
            "content": { "application/json": { "schema": { "type": "object", "additionalProperties": true } } }
          },
          "202": { "description": "Notification acceptée sans corps" }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": { "type": "http", "scheme": "bearer", "description": "Clé partenaire délivrée par Greffe-Entreprises." },
      "apiKeyAuth": { "type": "apiKey", "in": "header", "name": "X-API-Key", "description": "Alternative à Authorization: Bearer." }
    },
    "parameters": {
      "profile": { "name": "profile", "in": "query", "schema": { "type": "string", "enum": ["native", "dnb", "grand"] } }
    },
    "responses": {
      "BadRequest": {
        "description": "Requête invalide",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "Unauthorized": {
        "description": "Clé absente ou invalide",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "Forbidden": {
        "description": "Scope insuffisant",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "NotFound": {
        "description": "Entreprise introuvable",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "TooManyRequests": {
        "description": "Quota dépassé",
        "headers": { "Retry-After": { "schema": { "type": "integer" } } },
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "ServiceUnavailable": {
        "description": "Base indisponible",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "required": ["error"],
        "properties": {
          "error": {
            "type": "object",
            "required": ["code", "message"],
            "properties": {
              "code": { "type": "string" },
              "message": { "type": "string" },
              "documentation": { "type": "string", "format": "uri" }
            }
          }
        }
      },
      "Company": {
        "type": "object",
        "required": ["siren", "name"],
        "properties": {
          "siren": { "type": "string", "pattern": "^[0-9]{9}$" },
          "name": { "type": "string" },
          "legal_form": { "type": ["string", "null"] },
          "legal_form_code": { "type": ["string", "null"] },
          "city": { "type": ["string", "null"] },
          "created_at": { "type": ["string", "null"], "format": "date" },
          "siret_siege": { "type": ["string", "null"] },
          "ape": { "type": ["string", "null"] },
          "ape_label": { "type": ["string", "null"] },
          "adresse": { "type": ["string", "null"] },
          "etat": { "type": ["string", "null"] },
          "etat_label": { "type": ["string", "null"] },
          "nb_etablissements": { "type": ["integer", "null"] },
          "categorie": { "type": ["string", "null"] },
          "aliases": { "type": "array", "items": { "type": "string" } },
          "archive": {
            "type": "object",
            "properties": {
              "has_documents": { "type": "boolean" },
              "documents_count": { "type": "integer" }
            }
          },
          "urls": {
            "type": "object",
            "properties": {
              "html": { "type": "string", "format": "uri" },
              "json": { "type": "string", "format": "uri" },
              "official": { "type": "string", "format": "uri" }
            }
          },
          "sources": { "type": "array", "items": { "type": "string" } },
          "content_hash": { "type": "string" }
        }
      },
      "CompanyResponse": {
        "type": "object",
        "properties": {
          "data": {
            "allOf": [
              { "$ref": "#/components/schemas/Company" },
              {
                "type": "object",
                "properties": {
                  "registre": { "type": "object", "additionalProperties": true },
                  "related": { "type": "array", "items": { "type": "object", "additionalProperties": true } }
                }
              }
            ]
          },
          "meta": { "type": "object", "additionalProperties": true }
        }
      },
      "ListResponse": {
        "type": "object",
        "properties": {
          "data": { "type": "array", "items": { "$ref": "#/components/schemas/Company" } },
          "meta": { "type": "object", "additionalProperties": true },
          "links": { "type": "object", "additionalProperties": true }
        }
      }
    }
  }
}
