# `slice_openapi_spec` — Slice OpenAPI 3.0/3.1 or Swagger 2.0 Specification

> Part of [Spec Slice — OpenAPI & GraphQL Subgraph Extractor](https://spec-slice.apikeg.com/llms.txt) (`com.apikeg/spec-slice`)

Scores OpenAPI/Swagger endpoints against a natural-language `query` (BM25 + boosted `operationId`, `summary`, `tags`, and path tokens) or explicit `operationIds`/`pathPatterns`, walks every reachable JSON Pointer `$ref` with cycle protection, prunes unused paths/components, and emits ready-to-use `curl` + TypeScript `fetch` snippets.

**Why call instead of DIY:** Saves 50,000-250,000 prompt tokens per large enterprise OpenAPI spec ($0.15+ LLM input cost) while guaranteeing zero broken `#/components/...` `$ref` links or recursive schema crashes.

- **REST Endpoint:** `POST https://spec-slice.apikeg.com/v1/slice-openapi-spec`
- **MCP Server:** `https://spec-slice.apikeg.com/mcp` (tool: `slice_openapi_spec`)
- **Pricing:** $0.001 USD per OpenAPI/Swagger subgraph slice (x402 instant micropayment)
- **Free Dry-Run:** `POST https://spec-slice.apikeg.com/v1/slice-openapi-spec?dry_run=true`

## Input JSON Schema
```json
{
  "type": "object",
  "properties": {
    "spec": {
      "description": "OpenAPI 3.0/3.1 or Swagger 2.0 specification provided as either a parsed JSON object or a serialized JSON string (up to 5 MiB)."
    },
    "query": {
      "type": "string",
      "maxLength": 500,
      "description": "Natural-language task intent (e.g. 'create credit note and refund invoice') scored via Okapi BM25 plus boosted matches on operationId, summary, tags, and path segments."
    },
    "operationIds": {
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 1,
        "maxLength": 200
      },
      "maxItems": 50,
      "description": "Explicit operationId values to extract (case-insensitive)."
    },
    "pathPatterns": {
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 1,
        "maxLength": 300
      },
      "maxItems": 50,
      "description": "Explicit path templates or 'METHOD /v1/path/*' glob patterns to extract (e.g. 'POST /v1/invoices/*/refund')."
    },
    "methods": {
      "type": "array",
      "items": {
        "type": "string",
        "enum": [
          "GET",
          "POST",
          "PUT",
          "PATCH",
          "DELETE",
          "HEAD",
          "OPTIONS",
          "TRACE"
        ]
      },
      "maxItems": 8,
      "description": "Optional HTTP method allowlist."
    },
    "maxOperations": {
      "type": "integer",
      "default": 5,
      "minimum": 1,
      "maximum": 50,
      "description": "Maximum number of matched operations to retain in the sliced OpenAPI spec."
    },
    "compactDescriptions": {
      "type": "boolean",
      "default": false,
      "description": "Strip verbose prose descriptions, examples, and externalDocs while keeping spec-valid response descriptions."
    },
    "includeSnippets": {
      "type": "boolean",
      "default": true,
      "description": "Generate ready-to-use curl and TypeScript fetch code snippets for each selected operation."
    }
  },
  "required": [
    "spec"
  ],
  "additionalProperties": false
}
```

## Examples
### Extract refund & credit-note endpoints from billing OpenAPI spec
```bash
curl -X POST "https://spec-slice.apikeg.com/v1/slice-openapi-spec" \
  -H "Content-Type: application/json" \
  -d '{"query":"create credit note and refund invoice","maxOperations":2,"compactDescriptions":true,"includeSnippets":true,"spec":{"openapi":"3.1.0","info":{"title":"Billing API","version":"2.4.0"},"servers":[{"url":"https://api.billing.example.com"}],"paths":{"/v1/invoices/{invoiceId}/refunds":{"post":{"operationId":"refundInvoice","summary":"Refund an invoice","tags":["Invoices"],"requestBody":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/RefundRequest"}}}},"responses":{"200":{"description":"Refund created","content":{"application/json":{"schema":{"$ref":"#/components/schemas/Refund"}}}}}}},"/v1/warehouses":{"get":{"operationId":"listWarehouses","summary":"List inventory warehouses","responses":{"200":{"description":"OK","content":{"application/json":{"schema":{"$ref":"#/components/schemas/Warehouse"}}}}}}}},"components":{"schemas":{"RefundRequest":{"type":"object","required":["amountCents"],"properties":{"amountCents":{"type":"integer"}}},"Refund":{"type":"object","properties":{"id":{"type":"string"},"status":{"type":"string"}}},"Warehouse":{"type":"object","properties":{"id":{"type":"string"},"location":{"type":"string"}}}}}}}'
```
Expected output: Retains refundInvoice + RefundRequest + Refund, prunes listWarehouses + Warehouse, and emits curl + TypeScript fetch snippets.
