openapi: 3.1.0
info:
  title: NYC FHV Intelligence API
  version: 0.1.0
  summary: Commercial-grade access to NYC TLC For-Hire Vehicle (FHV) license data.
  description: |
    Turns NYC's TLC For-Hire Vehicles (FHV) - Active dataset (`8wbx-tsch`,
    refreshed daily by NYC TLC) into developer APIs.

    v0.1 ships two capabilities:

    1. **License Verification** — real-time "is this driver/vehicle TLC-active?"
       lookup by TLC license number, DMV plate, or VIN.
    2. **Renewal Radar** — query upcoming-expiration vehicles by date window,
       base, vehicle year, or wheelchair-accessible status; subscribe via
       webhook for daily push notifications.

    All requests require an API key issued at https://matchup.dev.
    Errors follow RFC 7807 (Problem Details for HTTP APIs).
  contact:
    name: API-Ventures @ Matchup Labs
    email: api@matchup.dev
    url: https://matchup.dev/api/fhv
  license:
    name: Commercial — see Terms of Service
    url: https://matchup.dev/legal/api-terms

servers:
  - url: https://fhv.matchup.dev/api/v1
    description: Production
  - url: https://fhv-staging.matchup.dev/api/v1
    description: Staging

security:
  - apiKeyAuth: []

tags:
  - name: Verification
    description: Real-time TLC license status checks.
  - name: Renewals
    description: Upcoming-expiration queries and webhook subscriptions.

paths:
  /verify/{identifier}:
    get:
      tags: [Verification]
      operationId: verifyLicense
      summary: Verify a TLC license by license number, plate, or VIN.
      description: |
        Returns active status and key license metadata for the supplied
        identifier. Cost: **1 credit** per call.

        The endpoint auto-detects identifier type:
        - TLC license number: alphanumeric, 6 chars (e.g., `C05015`)
        - DMV plate: 1–8 alphanumeric (e.g., `T438351`)
        - VIN: 17 alphanumeric chars

        Override detection with `?type=`.
      parameters:
        - $ref: '#/components/parameters/Identifier'
        - $ref: '#/components/parameters/IdentifierType'
      responses:
        '200':
          description: Verification result.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/VerificationResponse'
              examples:
                active_vehicle:
                  summary: Active vehicle by TLC license number
                  value:
                    data:
                      active: true
                      vehicle_license_number: "C05015"
                      name: "AYDIN, MEHMET"
                      license_type: "FOR HIRE VEHICLE"
                      expiration_date: "2026-08-16"
                      days_until_expiration: 107
                      dmv_license_plate_number: "T438351"
                      vehicle_vin_number: "1HGCM82633A004352"
                      wheelchair_accessible: false
                      vehicle_year: 2021
                      base_number: "B02765"
                      base_name: "UBER USA, LLC"
                      base_type: "BLACK CAR"
                    meta:
                      source: "nyc-open-data/tlc-fhv-active/8wbx-tsch"
                      updated: "2026-04-30T19:17:00Z"
                      query_ms: 184
                      credits_remaining: 24999
        '400':
          $ref: '#/components/responses/InvalidInput'
        '401':
          $ref: '#/components/responses/MissingApiKey'
        '403':
          $ref: '#/components/responses/KeyDisabled'
        '404':
          description: |
            No active record matches the identifier. Note: a 404 means the
            vehicle is not on the **active** list — it may exist as inactive,
            expired, or never-issued. v0.1 does not query the inactive dataset.
          content:
            application/problem+json:
              schema:
                $ref: '#/components/schemas/ProblemDetails'
              examples:
                not_found:
                  value:
                    type: "https://api.matchup.dev/errors/not_found"
                    title: "Not Found"
                    status: 404
                    detail: "No active TLC FHV record matches identifier 'C99999'."
        '429':
          $ref: '#/components/responses/RateLimited'
        '502':
          $ref: '#/components/responses/UpstreamError'

  /renewals/upcoming:
    get:
      tags: [Renewals]
      operationId: listUpcomingRenewals
      summary: List vehicles whose TLC license expires within N days.
      description: |
        Returns vehicles with `expiration_date` in `[today, today + days]`,
        optionally filtered by base, wheelchair status, or vehicle year.
        Paginated. Cost: **5 credits per page** (50 results).

        Use this for backfill / one-shot lead lists. For continuous push-mode
        delivery, create a subscription via `POST /renewals/subscriptions`.
      parameters:
        - $ref: '#/components/parameters/Days'
        - $ref: '#/components/parameters/BaseNumber'
        - $ref: '#/components/parameters/WheelchairAccessible'
        - $ref: '#/components/parameters/VehicleYearMax'
        - $ref: '#/components/parameters/Page'
        - $ref: '#/components/parameters/PageSize'
      responses:
        '200':
          description: Paginated list of vehicles in the renewal window.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/RenewalListResponse'
        '400':
          $ref: '#/components/responses/InvalidInput'
        '401':
          $ref: '#/components/responses/MissingApiKey'
        '429':
          $ref: '#/components/responses/RateLimited'

  /renewals/subscriptions:
    post:
      tags: [Renewals]
      operationId: createRenewalSubscription
      summary: Subscribe a webhook to daily renewal-window deliveries.
      description: |
        Creates a subscription. Each day after the dataset refresh
        (4–7 PM ET), the FHV pipeline diffs newly-entered renewal-window
        vehicles against yesterday's snapshot and POSTs the delta to
        `webhook_url`. Cost: **1 credit per delivered event**.

        Webhook payload is signed with HMAC-SHA256 over the raw body, using
        the `signing_secret` returned at creation. Header: `X-FHV-Signature`.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/SubscriptionCreate'
      responses:
        '201':
          description: Subscription created.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Subscription'
        '400':
          $ref: '#/components/responses/InvalidInput'
        '401':
          $ref: '#/components/responses/MissingApiKey'

    get:
      tags: [Renewals]
      operationId: listRenewalSubscriptions
      summary: List the calling key's renewal subscriptions.
      responses:
        '200':
          description: Array of subscriptions for the authenticated key.
          content:
            application/json:
              schema:
                type: object
                required: [data, meta]
                properties:
                  data:
                    type: array
                    items:
                      $ref: '#/components/schemas/Subscription'
                  meta:
                    $ref: '#/components/schemas/Meta'

  /renewals/subscriptions/{id}:
    delete:
      tags: [Renewals]
      operationId: deleteRenewalSubscription
      summary: Cancel a renewal subscription.
      parameters:
        - in: path
          name: id
          required: true
          schema:
            type: string
            format: uuid
      responses:
        '204':
          description: Subscription deleted.
        '401':
          $ref: '#/components/responses/MissingApiKey'
        '404':
          description: Subscription not found or not owned by this key.
          content:
            application/problem+json:
              schema:
                $ref: '#/components/schemas/ProblemDetails'

components:
  securitySchemes:
    apiKeyAuth:
      type: http
      scheme: bearer
      description: |
        Pass the API key in the `Authorization` header.
        Example: `Authorization: Bearer mv_live_abc123...`

  parameters:
    Identifier:
      in: path
      name: identifier
      required: true
      schema:
        type: string
        minLength: 1
        maxLength: 32
      description: TLC license number, DMV plate, or VIN.

    IdentifierType:
      in: query
      name: type
      required: false
      schema:
        type: string
        enum: [auto, license, plate, vin]
        default: auto
      description: Override identifier auto-detection.

    Days:
      in: query
      name: days
      required: false
      schema:
        type: integer
        minimum: 1
        maximum: 180
        default: 30
      description: Renewal window size in days from today.

    BaseNumber:
      in: query
      name: base_number
      required: false
      schema:
        type: string
      description: Filter by base number (e.g., `B02765`).

    WheelchairAccessible:
      in: query
      name: wheelchair_accessible
      required: false
      schema:
        type: boolean
      description: Filter to WAV-flagged vehicles only.

    VehicleYearMax:
      in: query
      name: vehicle_year_max
      required: false
      schema:
        type: integer
        minimum: 1990
        maximum: 2030
      description: |
        Return vehicles with `vehicle_year <= value`. Useful for stacking
        renewal + fleet-aging lead-gen filters.

    Page:
      in: query
      name: page
      required: false
      schema:
        type: integer
        minimum: 1
        default: 1

    PageSize:
      in: query
      name: page_size
      required: false
      schema:
        type: integer
        minimum: 10
        maximum: 100
        default: 50

  responses:
    InvalidInput:
      description: One or more parameters were invalid.
      content:
        application/problem+json:
          schema:
            $ref: '#/components/schemas/ProblemDetails'
    MissingApiKey:
      description: Authorization header missing or malformed.
      content:
        application/problem+json:
          schema:
            $ref: '#/components/schemas/ProblemDetails'
    KeyDisabled:
      description: API key disabled or expired.
      content:
        application/problem+json:
          schema:
            $ref: '#/components/schemas/ProblemDetails'
    RateLimited:
      description: Rate limit or credit ceiling exceeded.
      headers:
        Retry-After:
          schema:
            type: integer
          description: Seconds to wait before retrying.
      content:
        application/problem+json:
          schema:
            $ref: '#/components/schemas/ProblemDetails'
    UpstreamError:
      description: NYC TLC dataset upstream error or timeout.
      content:
        application/problem+json:
          schema:
            $ref: '#/components/schemas/ProblemDetails'

  schemas:
    Meta:
      type: object
      required: [source, updated, query_ms]
      properties:
        source:
          type: string
          example: "nyc-open-data/tlc-fhv-active/8wbx-tsch"
        updated:
          type: string
          format: date-time
          description: ISO timestamp of the upstream dataset's last refresh.
        query_ms:
          type: integer
          description: Server-side response time.
        credits_remaining:
          type: integer
          description: API key credits left after this request.

    ProblemDetails:
      type: object
      required: [type, title, status, detail]
      properties:
        type:
          type: string
          format: uri
        title:
          type: string
        status:
          type: integer
        detail:
          type: string
        instance:
          type: string

    Vehicle:
      type: object
      required:
        - active
        - vehicle_license_number
        - name
        - expiration_date
      properties:
        active:
          type: boolean
        vehicle_license_number:
          type: string
          example: "C05015"
        name:
          type: string
          example: "AYDIN, MEHMET"
        license_type:
          type: string
          example: "FOR HIRE VEHICLE"
        expiration_date:
          type: string
          format: date
        days_until_expiration:
          type: integer
          description: Negative if already expired.
        permit_license_number:
          type: string
          nullable: true
        dmv_license_plate_number:
          type: string
          nullable: true
        vehicle_vin_number:
          type: string
          nullable: true
        wheelchair_accessible:
          type: boolean
        vehicle_year:
          type: integer
          nullable: true
        base_number:
          type: string
          nullable: true
        base_name:
          type: string
          nullable: true
        base_type:
          type: string
          nullable: true
          example: "BLACK CAR"

    VerificationResponse:
      type: object
      required: [data, meta]
      properties:
        data:
          $ref: '#/components/schemas/Vehicle'
        meta:
          $ref: '#/components/schemas/Meta'

    RenewalListResponse:
      type: object
      required: [data, meta]
      properties:
        data:
          type: array
          items:
            $ref: '#/components/schemas/Vehicle'
        meta:
          allOf:
            - $ref: '#/components/schemas/Meta'
            - type: object
              required: [page, page_size, total_count]
              properties:
                page:
                  type: integer
                page_size:
                  type: integer
                total_count:
                  type: integer
                  description: Total matching records across all pages.

    SubscriptionFilter:
      type: object
      properties:
        days:
          type: integer
          minimum: 1
          maximum: 180
          default: 30
        base_number:
          type: string
        wheelchair_accessible:
          type: boolean
        vehicle_year_max:
          type: integer

    SubscriptionCreate:
      type: object
      required: [webhook_url, filter]
      properties:
        webhook_url:
          type: string
          format: uri
          description: HTTPS endpoint that will receive daily POST events.
        filter:
          $ref: '#/components/schemas/SubscriptionFilter'
        description:
          type: string
          maxLength: 200

    Subscription:
      allOf:
        - $ref: '#/components/schemas/SubscriptionCreate'
        - type: object
          required: [id, signing_secret, created_at, status]
          properties:
            id:
              type: string
              format: uuid
            signing_secret:
              type: string
              description: |
                HMAC-SHA256 secret. Returned **only** on creation — store it.
                Used to verify the `X-FHV-Signature` header on incoming events.
            status:
              type: string
              enum: [active, paused, failed]
            created_at:
              type: string
              format: date-time
            last_delivery_at:
              type: string
              format: date-time
              nullable: true
