openapi: 3.0.3
info:
  title: WeeURL API
  version: 1.0.0
  description: |
    Developer API for WeeURL — the Irish URL shortener. Create and manage short
    links on a .ie domain programmatically. Human-friendly docs: https://weeurl.ie/docs

    ## Authentication
    Send an API key on every request: `Authorization: Bearer <api_key>`.
    Keys look like `weeurl_<12 hex>_<48 hex>`. Sign in with GitHub at
    https://weeurl.ie/login, then create and revoke keys on your dashboard.
    A key is shown once, at creation. Free-plan keys need a GitHub account at
    least 30 days old. Keys are secrets — use them server-side only.

    ## Limits
    Plan, quota, and burst limit belong to the account, so every key on an
    account shares them.
    - **Burst:** 2 req/s (Free), 10 req/s (Pro), on every request. Exceeding it
      returns 429 with a `Retry-After` header.
    - **Monthly quota:** 100 (Free), 5,000 (Pro) successful link creations.
      Exceeding it returns 429 without `Retry-After`; the message includes the
      reset date. Quotas reset at 00:00 UTC on the 1st of each month.

    ## Responses
    Success bodies wrap the result in `data`. Errors are `{ "error": "<message>" }`
    with an appropriate status code — branch on the status code, not the message.
  contact:
    name: WeeURL
    url: https://weeurl.ie/about

servers:
  - url: https://weeurl.ie/api/v1
    description: Production
  - url: http://localhost:3000/api/v1
    description: Local development

security:
  - bearerAuth: []

tags:
  - name: Links
    description: Create, list, inspect, and disable short links
  - name: Account
    description: Plan and quota

paths:
  /links:
    post:
      tags: [Links]
      summary: Create a short link
      description: |
        The URL is validated (absolute http/https only) and screened with Google
        Safe Browsing — no bypass. Unsafe URLs are rejected with 400 and don't
        use quota. A successful create uses one link from the monthly quota.
      operationId: createLink
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [url]
              properties:
                url:
                  type: string
                  format: uri
                  description: Absolute http(s) URL to shorten. Stored normalised (e.g. a trailing `/` is added to a bare domain).
                  example: https://example.com/a/very/long/path
                slug:
                  $ref: "#/components/schemas/CustomSlug"
            examples:
              random:
                summary: Random slug
                value: { url: "https://example.com/a/very/long/path" }
              custom:
                summary: Custom slug (Pro)
                value: { url: "https://example.com/launch", slug: "launch-2026" }
      responses:
        "201":
          description: Link created
          content:
            application/json:
              schema:
                type: object
                properties:
                  data:
                    type: object
                    properties:
                      slug: { type: string, example: aB3dE9x }
                      short_url: { type: string, format: uri, example: "https://weeurl.ie/aB3dE9x" }
                      destination_url: { type: string, format: uri, example: "https://example.com/a/very/long/path" }
                      created_at: { type: string, format: date-time }
        "400":
          description: |
            Missing/invalid `url`, invalid or reserved `slug`, invalid JSON, the URL is flagged as unsafe, or the
            destination isn't allowed: another link shortener, a WeeURL link, an IP-logging site, a bare IP address,
            a local/private host, or a URL containing a username or password
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Error" }
              examples:
                invalidUrl: { value: { error: "Invalid URL format" } }
                shortener: { value: { error: "WeeURL doesn't shorten links from other link shorteners. Paste the full destination link instead." } }
                unsafe: { value: { error: "This URL has been flagged as unsafe" } }
                reserved: { value: { error: "This slug is reserved. Please choose another." } }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403":
          description: A `slug` was sent on the Free plan
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Error" }
              example: { error: "Custom slugs require a paid plan" }
        "409":
          description: The custom slug is already taken
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Error" }
              example: { error: "This slug is already taken" }
        "429": { $ref: "#/components/responses/RateLimitedOrQuota" }
        "503": { $ref: "#/components/responses/Unavailable" }

    get:
      tags: [Links]
      summary: List your links
      description: |
        Links created through the API by any key on the account, newest first.
        Links made on the anonymous weeurl.ie homepage are not included. A page
        past the end returns an empty `links` array.
      operationId: listLinks
      parameters:
        - name: page
          in: query
          schema: { type: integer, default: 1, minimum: 1 }
        - name: limit
          in: query
          description: Results per page (values above 50 are capped at 50)
          schema: { type: integer, default: 20, minimum: 1, maximum: 50 }
      responses:
        "200":
          description: A page of links
          content:
            application/json:
              schema:
                type: object
                properties:
                  data:
                    type: object
                    properties:
                      links:
                        type: array
                        items: { $ref: "#/components/schemas/Link" }
                      pagination:
                        type: object
                        properties:
                          page: { type: integer, example: 1 }
                          limit: { type: integer, example: 20 }
                          total: { type: integer, description: Total links on the account, example: 1 }
                          total_pages: { type: integer, example: 1 }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "429": { $ref: "#/components/responses/RateLimited" }

  /links/{slug}:
    parameters:
      - name: slug
        in: path
        required: true
        description: The link's slug (case-sensitive)
        schema: { type: string, example: aB3dE9x }

    get:
      tags: [Links]
      summary: Get one link
      description: |
        Returns 404 if the slug doesn't exist or belongs to another account, so
        other accounts' links can't be probed.
      operationId: getLink
      responses:
        "200":
          description: The link
          content:
            application/json:
              schema:
                type: object
                properties:
                  data: { $ref: "#/components/schemas/LinkDetail" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "404": { $ref: "#/components/responses/NotFound" }
        "429": { $ref: "#/components/responses/RateLimited" }

    patch:
      tags: [Links]
      summary: Disable or re-enable a link
      description: |
        A disabled link returns `410 Gone` to visitors instead of redirecting.
        The destination URL can't be changed. Links that have been reported for
        abuse (`reported: true`) can be disabled but not re-enabled (403).
        Disabling a link does not return quota.
      operationId: updateLink
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [status]
              properties:
                status: { type: string, enum: [active, disabled], example: disabled }
      responses:
        "200":
          description: Link updated
          content:
            application/json:
              schema:
                type: object
                properties:
                  data:
                    type: object
                    properties:
                      slug: { type: string, example: aB3dE9x }
                      status: { type: string, enum: [active, disabled], example: disabled }
        "400":
          description: "`status` is not `active` or `disabled`, or invalid JSON"
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Error" }
              example: { error: "Status must be 'active' or 'disabled'" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403":
          description: Tried to re-enable a link that has been reported for abuse
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Error" }
              example: { error: "This link has been reported and is under review" }
        "404": { $ref: "#/components/responses/NotFound" }
        "429": { $ref: "#/components/responses/RateLimited" }

  /links/{slug}/qr:
    parameters:
      - name: slug
        in: path
        required: true
        description: The link's slug (case-sensitive)
        schema: { type: string, example: aB3dE9x }
      - name: format
        in: query
        required: false
        description: Image format. PNG is 1024px square; SVG scales to any size.
        schema: { type: string, enum: [png, svg], default: png }

    get:
      tags: [Links]
      summary: Get a link's QR code
      description: |
        **Pro plan only** (Free returns 403).
        Returns the QR code image directly (not JSON). The code encodes the
        short link (`https://weeurl.ie/<slug>?src=qr`), never the destination,
        so scans count as clicks and stop working if the link is disabled.
        Generated fresh on each request; doesn't use any quota.
      operationId: getLinkQr
      responses:
        "200":
          description: The QR code image
          content:
            image/png:
              schema: { type: string, format: binary }
            image/svg+xml:
              schema: { type: string }
        "400":
          description: "`format` is not `png` or `svg`"
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Error" }
              example: { error: "Format must be 'png' or 'svg'" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "403":
          description: Called on the Free plan
          content:
            application/json:
              schema: { $ref: "#/components/schemas/Error" }
              example: { error: "QR codes via the API require a paid plan" }
        "404": { $ref: "#/components/responses/NotFound" }
        "429": { $ref: "#/components/responses/RateLimited" }

  /usage:
    get:
      tags: [Account]
      summary: Check your plan and quota
      description: Doesn't use any quota — a cheap way to check a key works.
      operationId: getUsage
      responses:
        "200":
          description: Plan and usage for the current month
          content:
            application/json:
              schema:
                type: object
                properties:
                  data:
                    type: object
                    properties:
                      tier: { type: string, enum: [free, paid], description: "`paid` is the Pro plan", example: free }
                      links_used: { type: integer, description: Links created this calendar month (UTC), example: 12 }
                      links_limit: { type: integer, description: Monthly link quota for the plan, example: 100 }
                      burst_limit: { type: integer, description: Requests per second allowed, example: 2 }
                      reset_date: { type: string, format: date-time, description: "00:00 UTC on the 1st of next month", example: "2026-10-01T00:00:00.000Z" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "429": { $ref: "#/components/responses/RateLimited" }
        "503": { $ref: "#/components/responses/Unavailable" }

components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: "weeurl_<12 hex>_<48 hex>"

  schemas:
    CustomSlug:
      type: string
      description: |
        Custom slug — **Pro plan only** (Free returns 403). 3–32 letters,
        numbers, hyphens, or underscores; case-sensitive. Words the site uses
        itself (e.g. `docs`, `admin`, `api`) are reserved. Omit for a random
        7-character slug.
      pattern: "^[a-zA-Z0-9_-]{3,32}$"
      example: launch-2026

    Link:
      type: object
      properties:
        slug: { type: string, example: aB3dE9x }
        destination_url: { type: string, format: uri, example: "https://example.com/a/very/long/path" }
        status:
          type: string
          enum: [active, disabled]
          description: "`disabled` links return 410 Gone to visitors"
        click_count: { type: integer, description: Number of times the short URL has redirected a visitor, example: 42 }
        created_at: { type: string, format: date-time }
        reported:
          type: boolean
          description: Someone has reported this link for abuse; it can't be re-enabled while under review

    LinkDetail:
      allOf:
        - $ref: "#/components/schemas/Link"
        - type: object
          properties:
            short_url: { type: string, format: uri, example: "https://weeurl.ie/aB3dE9x" }

    Error:
      type: object
      required: [error]
      properties:
        error: { type: string, description: Human-readable message (may be reworded — branch on the status code) }

  responses:
    Unauthorized:
      description: |
        No `Authorization` header, or the key is malformed, unknown, or revoked.
        The last three return the same message so a response never reveals
        whether a key exists.
      content:
        application/json:
          schema: { $ref: "#/components/schemas/Error" }
          examples:
            missing: { value: { error: "Missing or invalid Authorization header. Expected: Bearer <api_key>" } }
            invalid: { value: { error: "Invalid API key" } }
    NotFound:
      description: The link doesn't exist or belongs to another account
      content:
        application/json:
          schema: { $ref: "#/components/schemas/Error" }
          example: { error: "Link not found" }
    RateLimited:
      description: Burst limit exceeded (per account, per second). Wait `Retry-After` seconds and retry.
      headers:
        Retry-After:
          description: Seconds to wait before retrying
          schema: { type: integer, example: 1 }
      content:
        application/json:
          schema: { $ref: "#/components/schemas/Error" }
          example: { error: "Rate limit exceeded. Please slow down." }
    RateLimitedOrQuota:
      description: |
        Either the burst limit (has `Retry-After`; retry shortly) or the monthly
        quota (no `Retry-After`; don't retry until the reset date in the message).
      headers:
        Retry-After:
          description: Present for the burst limit only — seconds to wait
          schema: { type: integer, example: 1 }
      content:
        application/json:
          schema: { $ref: "#/components/schemas/Error" }
          examples:
            burst: { value: { error: "Rate limit exceeded. Please slow down." } }
            quota: { value: { error: "Monthly quota exceeded. Upgrade or wait until 2026-10-01T00:00:00.000Z" } }
    Unavailable:
      description: Quota service briefly unavailable. Safe to retry with backoff.
      content:
        application/json:
          schema: { $ref: "#/components/schemas/Error" }
          example: { error: "Service temporarily unavailable. Please retry shortly." }
