{
  "openapi": "3.1.0",
  "info": {
    "title": "Valinweb QR image API",
    "version": "1.2.0",
    "description": "Une route QR : GET sans clé ou avec Bearer, POST JSON avec Bearer. Une sauvegarde facultative via save crée un QR dans la bibliothèque du compte. Le GET reste sans effet de sauvegarde."
  },
  "servers": [
    {
      "url": "/",
      "description": "Origine de l’installation servant cette documentation."
    }
  ],
  "paths": {
    "/api/qr/status": {
      "get": {
        "summary": "Activation et limites publiques ; ne garantit pas la disponibilité de Redis.",
        "security": [],
        "responses": {
          "200": {
            "description": "État public",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "version": {
                      "const": 1
                    },
                    "enabled": {
                      "type": "boolean"
                    },
                    "types": {
                      "type": "array",
                      "items": {
                        "type": "string"
                      }
                    },
                    "formats": {
                      "type": "array",
                      "items": {
                        "type": "string"
                      }
                    },
                    "limits": {
                      "type": "object"
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/api/qr": {
      "get": {
        "summary": "Image PNG ou SVG",
        "security": [
          {},
          {
            "ApiKeyBearer": []
          }
        ],
        "parameters": [
          {
            "name": "content",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string",
              "minLength": 1,
              "maxLength": 1024
            },
            "description": "Maximum 1 024 octets UTF-8 ; URL totale de 4 096 octets."
          },
          {
            "name": "type",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "enum": [
                "url",
                "text"
              ],
              "default": "url"
            },
            "description": ""
          },
          {
            "name": "format",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "enum": [
                "png",
                "svg"
              ],
              "default": "png"
            },
            "description": ""
          },
          {
            "name": "size",
            "in": "query",
            "required": false,
            "schema": {
              "type": "integer",
              "minimum": 128,
              "maximum": 1024,
              "default": 300
            },
            "description": "Largeur et hauteur de l’image carrée en pixels. La signature valinweb.fr occupe une fine bande réservée sous le motif, à l’intérieur de ces dimensions."
          },
          {
            "name": "dark",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "pattern": "^[a-fA-F0-9]{6}$",
              "default": "000000"
            },
            "description": ""
          },
          {
            "name": "light",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "pattern": "^[a-fA-F0-9]{6}$",
              "default": "ffffff"
            },
            "description": ""
          },
          {
            "name": "ecc",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "enum": [
                "L",
                "M",
                "Q",
                "H"
              ],
              "default": "M"
            },
            "description": ""
          },
          {
            "name": "margin",
            "in": "query",
            "required": false,
            "schema": {
              "type": "integer",
              "minimum": 0,
              "maximum": 8,
              "default": 2
            },
            "description": "Marge en modules, de 0 à 8 ; la signature occupe un espace réservé indépendant."
          },
          {
            "name": "style",
            "in": "query",
            "required": false,
            "schema": {
              "type": "string",
              "enum": [
                "square"
              ],
              "default": "square"
            },
            "description": ""
          }
        ],
        "responses": {
          "200": {
            "description": "Image générée, sans compte ni sauvegarde.",
            "headers": {
              "ETag": {
                "schema": {
                  "type": "string"
                }
              },
              "Cache-Control": {
                "schema": {
                  "type": "string"
                }
              }
            },
            "content": {
              "image/png": {
                "schema": {
                  "type": "string",
                  "format": "binary"
                }
              },
              "image/svg+xml": {
                "schema": {
                  "type": "string",
                  "format": "binary"
                }
              }
            }
          },
          "304": {
            "description": "Image inchangée ; le quota est appliqué avant revalidation."
          },
          "400": {
            "description": "Paramètre inconnu/répété, contenu ou contraste invalide.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "413": {
            "description": "URL trop longue.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "422": {
            "description": "Contenu trop dense pour ces réglages.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Quota atteint.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            },
            "headers": {
              "Retry-After": {
                "schema": {
                  "type": "integer"
                }
              }
            }
          },
          "503": {
            "description": "API désactivée ou service momentanément indisponible.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            },
            "headers": {
              "Retry-After": {
                "schema": {
                  "type": "integer"
                }
              }
            }
          }
        }
      },
      "post": {
        "summary": "Image privée, sans cache ou sauvegarde, avec une clé de rendu",
        "security": [
          {
            "ApiKeyBearer": []
          }
        ],
        "description": "POST sans query ; quotas par compte et clé avant rendu. Aucun cookie requis. 3 clés actives/5 créations par jour, 60/minute et 1000/jour/compte, 30/minute et 500/jour/clé ; plafonds inférieurs possibles. Budgets UTC. Une demande autorisée est comptée même si le rendu échoue. Protection IP 60/minute et 2000/jour, plafond global partagé 120/minute.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/RenderInput"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Image privée ; Cache-Control private,no-store, aucun ETag.",
            "content": {
              "image/png": {
                "schema": {
                  "type": "string",
                  "format": "binary"
                }
              },
              "image/svg+xml": {
                "schema": {
                  "type": "string",
                  "format": "binary"
                }
              }
            },
            "headers": {
              "X-QR-Code-Id": {
                "description": "Identifiant du QR dans le compte, uniquement si save est fourni.",
                "schema": {
                  "type": "string"
                }
              }
            }
          },
          "400": {
            "description": "Corps invalide ou query interdite.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Clé manquante, invalide, expirée, révoquée ou compte sans accès.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Rôle sans permission de rendu.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "413": {
            "description": "Corps supérieur à 8 Kio.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "422": {
            "description": "Densité incompatible avec les réglages.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Quota atteint.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            },
            "headers": {
              "Retry-After": {
                "schema": {
                  "type": "integer"
                }
              }
            }
          },
          "503": {
            "description": "API désactivée ou rendu indisponible.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/api/account/api-keys": {
      "get": {
        "summary": "Métadonnées des clés et quotas du compte",
        "security": [
          {
            "accountSession": []
          }
        ],
        "responses": {
          "200": {
            "description": "Clés actives et historique borné, limites et usage UTC ; aucun secret/digest."
          },
          "401": {
            "description": "Session requise.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Fonctionnalité ou rôle indisponible.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      },
      "post": {
        "summary": "Créer une clé ; secret retourné une seule fois",
        "security": [
          {
            "accountSession": [],
            "csrf": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "additionalProperties": false,
                "required": [
                  "name",
                  "days"
                ],
                "properties": {
                  "name": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 80
                  },
                  "days": {
                    "type": "integer",
                    "enum": [
                      30,
                      90,
                      365
                    ]
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Conservez le secret côté serveur ; aucune réémission après perte de la réponse.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "key": {
                      "$ref": "#/components/schemas/QrApiKey"
                    },
                    "secret": {
                      "type": "string",
                      "description": "Affiché uniquement dans cette réponse."
                    }
                  }
                }
              }
            }
          },
          "403": {
            "description": "OTP récent (<10 min), origine/CSRF et permission nécessaires.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "Nombre de clés ou créations journalières atteint.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/api/account/api-keys/{id}": {
      "delete": {
        "summary": "Révoquer sa clé après OTP récent",
        "security": [
          {
            "accountSession": [],
            "csrf": []
          }
        ],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Les prochaines autorisations sont refusées ; un rendu déjà autorisé peut se terminer."
          },
          "403": {
            "description": "Connexion récente, origine, CSRF et rôle requis.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Clé absente ou appartenant à un autre compte.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Error": {
        "type": "object",
        "required": [
          "error"
        ],
        "properties": {
          "error": {
            "type": "object",
            "required": [
              "code",
              "message"
            ],
            "properties": {
              "code": {
                "type": "string"
              },
              "message": {
                "type": "string"
              },
              "fields": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "requestId": {
                "type": "string",
                "format": "uuid"
              },
              "issues": {
                "type": "array",
                "items": {
                  "type": "object",
                  "required": [
                    "field",
                    "code",
                    "message"
                  ],
                  "properties": {
                    "field": {
                      "type": "string",
                      "description": "Nom du paramètre URL ou chemin JSON, par exemple payload.ssid."
                    },
                    "code": {
                      "type": "string",
                      "description": "required, type, additionalProperties, pattern…"
                    },
                    "message": {
                      "type": "string",
                      "description": "Explication sans recopier la valeur transmise."
                    }
                  }
                }
              }
            }
          }
        }
      },
      "RenderInput": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "schemaVersion",
          "type",
          "payload"
        ],
        "properties": {
          "schemaVersion": {
            "type": "integer",
            "const": 1
          },
          "type": {
            "type": "string",
            "enum": [
              "url",
              "text",
              "email",
              "phone",
              "sms",
              "wifi",
              "contact"
            ]
          },
          "payload": {
            "type": "object"
          },
          "save": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "name",
              "requestId"
            ],
            "properties": {
              "name": {
                "type": "string",
                "minLength": 1,
                "maxLength": 100,
                "pattern": "\\S"
              },
              "requestId": {
                "type": "string",
                "pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$"
              },
              "mode": {
                "type": "string",
                "enum": [
                  "static",
                  "dynamic"
                ],
                "default": "static"
              }
            }
          },
          "options": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
              "size": {
                "type": "string",
                "pattern": "^(?:12[89]|1[3-9][0-9]|[2-9][0-9]{2}|10[01][0-9]|102[0-4])$",
                "description": "Largeur et hauteur de l’image carrée en pixels. La signature valinweb.fr occupe une fine bande réservée sous le motif, à l’intérieur de ces dimensions."
              },
              "color": {
                "default": "#000000",
                "type": "string",
                "pattern": "^#[a-fA-F0-9]{6}$"
              },
              "bgColor": {
                "default": "#ffffff",
                "type": "string",
                "pattern": "^#[a-fA-F0-9]{6}$"
              },
              "correction": {
                "default": "M",
                "type": "string",
                "enum": [
                  "L",
                  "M",
                  "Q",
                  "H"
                ]
              },
              "margin": {
                "default": 2,
                "type": "integer",
                "minimum": 0,
                "maximum": 8
              }
            }
          },
          "format": {
            "type": "string",
            "enum": [
              "png",
              "svg"
            ]
          }
        },
        "allOf": [
          {
            "if": {
              "properties": {
                "type": {
                  "const": "url"
                }
              },
              "required": [
                "type"
              ]
            },
            "then": {
              "properties": {
                "payload": {
                  "type": "object",
                  "additionalProperties": false,
                  "properties": {
                    "url": {
                      "type": "string",
                      "maxLength": 4096,
                      "minLength": 1,
                      "pattern": "^(?:$|https?://[^\\s]+)$"
                    }
                  },
                  "required": [
                    "url"
                  ]
                }
              }
            }
          },
          {
            "if": {
              "properties": {
                "type": {
                  "const": "text"
                }
              },
              "required": [
                "type"
              ]
            },
            "then": {
              "properties": {
                "payload": {
                  "type": "object",
                  "additionalProperties": false,
                  "properties": {
                    "text": {
                      "type": "string",
                      "maxLength": 4096,
                      "minLength": 1
                    }
                  },
                  "required": [
                    "text"
                  ]
                }
              }
            }
          },
          {
            "if": {
              "properties": {
                "type": {
                  "const": "email"
                }
              },
              "required": [
                "type"
              ]
            },
            "then": {
              "properties": {
                "payload": {
                  "type": "object",
                  "additionalProperties": false,
                  "properties": {
                    "receiver": {
                      "type": "string",
                      "maxLength": 4096,
                      "minLength": 1,
                      "pattern": "^(?:$|[^\\s@<>]+@[^\\s@<>]+\\.[^\\s@<>]+)$"
                    },
                    "subject": {
                      "type": "string",
                      "maxLength": 4096,
                      "minLength": 1
                    },
                    "message": {
                      "type": "string",
                      "maxLength": 4096,
                      "minLength": 1
                    }
                  },
                  "required": [
                    "receiver",
                    "subject",
                    "message"
                  ]
                }
              }
            }
          },
          {
            "if": {
              "properties": {
                "type": {
                  "const": "phone"
                }
              },
              "required": [
                "type"
              ]
            },
            "then": {
              "properties": {
                "payload": {
                  "type": "object",
                  "additionalProperties": false,
                  "properties": {
                    "phone": {
                      "type": "string",
                      "maxLength": 4096,
                      "minLength": 1
                    }
                  },
                  "required": [
                    "phone"
                  ]
                }
              }
            }
          },
          {
            "if": {
              "properties": {
                "type": {
                  "const": "sms"
                }
              },
              "required": [
                "type"
              ]
            },
            "then": {
              "properties": {
                "payload": {
                  "type": "object",
                  "additionalProperties": false,
                  "properties": {
                    "phone": {
                      "type": "string",
                      "maxLength": 4096,
                      "minLength": 1
                    },
                    "message": {
                      "type": "string",
                      "maxLength": 4096,
                      "minLength": 1
                    }
                  },
                  "required": [
                    "phone",
                    "message"
                  ]
                }
              }
            }
          },
          {
            "if": {
              "properties": {
                "type": {
                  "const": "wifi"
                }
              },
              "required": [
                "type"
              ]
            },
            "then": {
              "properties": {
                "payload": {
                  "type": "object",
                  "additionalProperties": false,
                  "properties": {
                    "ssid": {
                      "type": "string",
                      "maxLength": 4096,
                      "minLength": 1
                    },
                    "security": {
                      "type": "string",
                      "maxLength": 4096,
                      "minLength": 1,
                      "enum": [
                        "WPA",
                        "WEP",
                        "nopass"
                      ]
                    },
                    "password": {
                      "type": "string",
                      "maxLength": 4096
                    }
                  },
                  "required": [
                    "ssid",
                    "security"
                  ],
                  "allOf": [
                    {
                      "if": {
                        "properties": {
                          "security": {
                            "enum": [
                              "WPA",
                              "WEP"
                            ]
                          }
                        },
                        "required": [
                          "security"
                        ]
                      },
                      "then": {
                        "properties": {
                          "password": {
                            "type": "string",
                            "minLength": 1
                          }
                        },
                        "required": [
                          "password"
                        ]
                      }
                    }
                  ]
                }
              }
            }
          },
          {
            "if": {
              "properties": {
                "type": {
                  "const": "contact"
                }
              },
              "required": [
                "type"
              ]
            },
            "then": {
              "properties": {
                "payload": {
                  "type": "object",
                  "additionalProperties": false,
                  "properties": {
                    "name": {
                      "type": "string",
                      "maxLength": 4096,
                      "minLength": 1,
                      "pattern": "^[^\\r\\n]*$"
                    },
                    "organization": {
                      "type": "string",
                      "maxLength": 4096,
                      "pattern": "^[^\\r\\n]*$"
                    },
                    "phone": {
                      "type": "string",
                      "maxLength": 4096,
                      "pattern": "^[^\\r\\n]*$"
                    },
                    "email": {
                      "type": "string",
                      "maxLength": 4096,
                      "pattern": "^(?:$|[^\\s@<>]+@[^\\s@<>]+\\.[^\\s@<>]+)$"
                    },
                    "website": {
                      "type": "string",
                      "maxLength": 4096,
                      "pattern": "^(?:$|https?://[^\\s]+)$"
                    }
                  },
                  "required": [
                    "name"
                  ]
                }
              }
            }
          }
        ]
      },
      "QrApiKey": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "prefix": {
            "type": "string"
          },
          "scopes": {
            "type": "array",
            "items": {
              "const": "qr:render"
            }
          },
          "state": {
            "enum": [
              "active",
              "expired",
              "revoked"
            ]
          },
          "createdAt": {
            "type": "string",
            "format": "date-time"
          },
          "expiresAt": {
            "type": "string",
            "format": "date-time"
          },
          "revokedAt": {
            "type": [
              "string",
              "null"
            ],
            "format": "date-time"
          },
          "lastUsedAt": {
            "type": [
              "string",
              "null"
            ],
            "format": "date-time"
          }
        }
      }
    },
    "securitySchemes": {
      "ApiKeyBearer": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "viw_api_<identifiant>.<secret>"
      },
      "accountSession": {
        "type": "apiKey",
        "in": "cookie",
        "name": "__Host-viw_account_session"
      },
      "csrf": {
        "type": "apiKey",
        "in": "header",
        "name": "x-account-csrf"
      }
    }
  }
}
