{
  "openapi": "3.1.0",
  "info": {
    "title": "Legaldigm Legal API",
    "description": "Legaldigms rättsdatabas som API: svensk och europeisk lagtext per paragraf, domstolsavgöranden, internationella avgöranden och juridiska AI-svar med källhänvisning. Varje anrop kräver en API-nyckel och dras från kontots förbetalda tokensaldo. Skapa nyckel och köp tokenpaket på https://legaldigm.se/api-nycklar.",
    "version": "1.0.0",
    "contact": {
      "name": "Legaldigm",
      "url": "https://legaldigm.se/kontakt"
    },
    "termsOfService": "https://legaldigm.se/villkor"
  },
  "servers": [
    {
      "url": "https://zdpjpohbmxiaykcdswsb.supabase.co/functions/v1/legal-api",
      "description": "Legaldigm produktion"
    },
    {
      "url": "https://zdpjpohbmxiaykcdswsb.supabase.co/functions/v1/agent-account",
      "description": "Kontoregistrering och betalning för AI-agenter"
    }
  ],
  "security": [
    {
      "OAuth2": [
        "openid",
        "email",
        "profile"
      ]
    },
    {
      "ApiKeyAuth": []
    }
  ],
  "paths": {
    "/v1/search": {
      "post": {
        "operationId": "searchLegalSources",
        "summary": "Sök i Legaldigms rättskällor",
        "description": "Söker i svensk lagtext per paragraf, svenska domstolsavgöranden och internationella avgöranden. Returnerar utdrag med källhänvisning som kan citeras.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "query"
                ],
                "properties": {
                  "query": {
                    "type": "string",
                    "description": "Juridisk fråga eller sökord, t.ex. 'uppsägning personliga skäl'."
                  },
                  "limit": {
                    "type": "integer",
                    "description": "Antal träffar per källtyp. Standard 8.",
                    "default": 8
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Träffar med källhänvisning",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "tool": {
                      "type": "string"
                    },
                    "source": {
                      "type": "string"
                    },
                    "tokens_billed": {
                      "type": "integer"
                    },
                    "balance": {
                      "type": "integer"
                    },
                    "results": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/LegalSource"
                      }
                    }
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "402": {
            "$ref": "#/components/responses/PaymentRequired"
          }
        }
      }
    },
    "/v1/law": {
      "post": {
        "operationId": "getLawSection",
        "summary": "Hämta ordagrann svensk lagtext",
        "description": "Hämtar författningstext för ett SFS-nummer, valfritt begränsat till en enskild paragraf.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "sfs_number"
                ],
                "properties": {
                  "sfs_number": {
                    "type": "string",
                    "description": "SFS-nummer, t.ex. '1982:80'."
                  },
                  "section_number": {
                    "type": "string",
                    "description": "Paragrafnummer, t.ex. '7' eller '7 a'."
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Författning och paragrafer",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "law": {
                      "type": "object",
                      "properties": {
                        "id": {
                          "type": "string"
                        },
                        "sfs_number": {
                          "type": "string"
                        },
                        "title": {
                          "type": "string"
                        },
                        "short_title": {
                          "type": "string"
                        }
                      }
                    },
                    "sections": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "section_number": {
                            "type": "string"
                          },
                          "heading": {
                            "type": "string"
                          },
                          "content": {
                            "type": "string"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "402": {
            "$ref": "#/components/responses/PaymentRequired"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      }
    },
    "/v1/judgment": {
      "post": {
        "operationId": "getJudgment",
        "summary": "Hämta ett domstolsavgörande",
        "description": "Hämtar ett avgörande via målnummer eller Legaldigm-id, med domstol, datum, sammanfattning och domtext.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "case_number": {
                    "type": "string",
                    "description": "Målnummer, t.ex. 'T 1234-22'."
                  },
                  "judgment_id": {
                    "type": "string",
                    "description": "Legaldigms interna id för avgörandet."
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Avgörandet",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "judgment": {
                      "type": "object",
                      "properties": {
                        "id": {
                          "type": "string"
                        },
                        "case_number": {
                          "type": "string"
                        },
                        "court_name": {
                          "type": "string"
                        },
                        "judgment_date": {
                          "type": "string"
                        },
                        "title": {
                          "type": "string"
                        },
                        "summary": {
                          "type": "string"
                        },
                        "full_text": {
                          "type": "string"
                        },
                        "legal_areas": {
                          "type": "array",
                          "items": {
                            "type": "string"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "402": {
            "$ref": "#/components/responses/PaymentRequired"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      }
    },
    "/v1/ask": {
      "post": {
        "operationId": "askLegalAi",
        "summary": "Ställ en juridisk fråga till Legaldigm",
        "description": "Ger ett fullständigt juridiskt svar med rubrikerna Underlag, Tolkning, Rättsligt stöd och Rekommendation. Svaret bygger enbart på Legaldigms rättskällor och redovisar dem.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "question"
                ],
                "properties": {
                  "question": {
                    "type": "string",
                    "description": "Den juridiska frågan, på svenska eller engelska."
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Juridiskt svar med källor",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "answer": {
                      "type": "string"
                    },
                    "sources": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/LegalSource"
                      }
                    },
                    "source": {
                      "type": "string"
                    },
                    "tokens_billed": {
                      "type": "integer"
                    },
                    "balance": {
                      "type": "integer"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "402": {
            "$ref": "#/components/responses/PaymentRequired"
          },
          "429": {
            "description": "För många anrop. Försök igen om en stund."
          }
        }
      }
    },
    "/v1/agents/register": {
      "post": {
        "operationId": "registerAgentAccount",
        "summary": "Registrera ett Legaldigm-konto som AI-agent",
        "description": "Skapar ett eget Legaldigm-konto, returnerar en API-nyckel (visas bara en gång) och en betalningslänk för valt tokenpaket. Kräver ingen befintlig inloggning. Anropen mot API:et fungerar först när betalningen är genomförd.",
        "security": [],
        "servers": [
          {
            "url": "https://zdpjpohbmxiaykcdswsb.supabase.co/functions/v1/agent-account"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "email"
                ],
                "properties": {
                  "email": {
                    "type": "string",
                    "format": "email",
                    "description": "E-postadress som kontot knyts till."
                  },
                  "agent_name": {
                    "type": "string",
                    "description": "Namn på agenten eller integrationen."
                  },
                  "package": {
                    "type": "string",
                    "enum": [
                      "small",
                      "medium",
                      "large"
                    ],
                    "description": "Tokenpaket: small 1M/999 kr, medium 5M/4 499 kr, large 20M/15 999 kr."
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Konto skapat med nyckel och betalningslänk",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "api_key": {
                      "type": "string"
                    },
                    "checkout_url": {
                      "type": "string"
                    },
                    "balance_tokens": {
                      "type": "integer"
                    },
                    "payment_required": {
                      "type": "boolean"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Ogiltig e-postadress eller okänt paket."
          },
          "409": {
            "description": "E-postadressen har redan ett konto."
          }
        }
      }
    },
    "/v1/agents/topup": {
      "post": {
        "operationId": "topUpAgentAccount",
        "summary": "Köp fler tokens",
        "description": "Returnerar en betalningslänk för ett nytt tokenpaket till det konto nyckeln tillhör.",
        "servers": [
          {
            "url": "https://zdpjpohbmxiaykcdswsb.supabase.co/functions/v1/agent-account"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "package": {
                    "type": "string",
                    "enum": [
                      "small",
                      "medium",
                      "large"
                    ]
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Betalningslänk",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "checkout_url": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    },
    "/v1/agents/status": {
      "post": {
        "operationId": "getAgentAccountStatus",
        "summary": "Kontostatus och saldo",
        "description": "Visar återstående tokens och total användning för nyckelns konto.",
        "servers": [
          {
            "url": "https://zdpjpohbmxiaykcdswsb.supabase.co/functions/v1/agent-account"
          }
        ],
        "responses": {
          "200": {
            "description": "Saldo och användning",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "balance_tokens": {
                      "type": "integer"
                    },
                    "lifetime_used_tokens": {
                      "type": "integer"
                    },
                    "active": {
                      "type": "boolean"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "ApiKeyAuth": {
        "type": "apiKey",
        "in": "header",
        "name": "x-api-key",
        "description": "API-nyckel från https://legaldigm.se/api-nycklar. Anrop utan giltig nyckel avvisas med 401."
      },
      "OAuth2": {
        "type": "oauth2",
        "description": "Logga in med ditt Legaldigm-konto (ChatGPT-plugin / GPT Actions).",
        "flows": {
          "authorizationCode": {
            "authorizationUrl": "https://zdpjpohbmxiaykcdswsb.supabase.co/auth/v1/oauth/authorize",
            "tokenUrl": "https://zdpjpohbmxiaykcdswsb.supabase.co/auth/v1/oauth/token",
            "scopes": {
              "openid": "Identitet",
              "email": "E-post",
              "profile": "Profil"
            }
          }
        }
      }
    },
    "schemas": {
      "LegalSource": {
        "type": "object",
        "properties": {
          "type": {
            "type": "string",
            "description": "legislation, judgment eller international_case."
          },
          "title": {
            "type": "string"
          },
          "reference": {
            "type": "string",
            "description": "SFS-nummer, målnummer eller ECLI."
          },
          "excerpt": {
            "type": "string"
          },
          "url": {
            "type": "string",
            "description": "Länk till källan på legaldigm.se."
          }
        }
      },
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "string"
          }
        }
      }
    },
    "responses": {
      "Unauthorized": {
        "description": "Saknad, ogiltig eller spärrad API-nyckel.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "PaymentRequired": {
        "description": "Tokensaldot räcker inte. Fyll på tokenpaket på legaldigm.se/api-nycklar.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "NotFound": {
        "description": "Källan finns inte i databasen.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      }
    }
  }
}