{
  "openapi": "3.1.0",
  "info": {
    "title": "Kernarc API",
    "version": "1.0.0",
    "description": "OpenAI-compatible inference with prepaid USD credit. Top up with USDC over x402 (Base Sepolia (testnet)). TEST MODE: mock model, testnet USDC.",
    "contact": {
      "email": "hello@kernarc.com"
    }
  },
  "servers": [
    {
      "url": "https://api.kernarc.com/v1"
    }
  ],
  "components": {
    "securitySchemes": {
      "bearer": {
        "type": "http",
        "scheme": "bearer",
        "description": "API key from POST /accounts"
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "object",
            "properties": {
              "message": {
                "type": "string"
              },
              "type": {
                "type": "string"
              },
              "code": {
                "type": "string"
              },
              "hint": {
                "type": "string"
              }
            }
          }
        }
      },
      "Account": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "balance_usd": {
            "type": "string"
          },
          "trial_credit_usd": {
            "type": "string"
          },
          "created_at": {
            "type": "string"
          }
        }
      }
    }
  },
  "paths": {
    "/accounts": {
      "post": {
        "summary": "Create an account and an API key",
        "requestBody": {
          "required": false,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "label": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Created. api_key is shown once.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "account": {
                      "$ref": "#/components/schemas/Account"
                    },
                    "api_key": {
                      "type": "string"
                    },
                    "api_key_id": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "429": {
            "description": "Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/models": {
      "get": {
        "summary": "List models with prices",
        "responses": {
          "200": {
            "description": "OpenAI model list with pricing"
          }
        }
      }
    },
    "/chat/completions": {
      "post": {
        "summary": "OpenAI-compatible chat completion",
        "security": [
          {
            "bearer": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "messages"
                ],
                "properties": {
                  "model": {
                    "type": "string",
                    "default": "qwen3.8-27b"
                  },
                  "messages": {
                    "type": "array",
                    "items": {
                      "type": "object"
                    }
                  },
                  "max_tokens": {
                    "type": "integer",
                    "default": 4096,
                    "maximum": 32768
                  },
                  "stream": {
                    "type": "boolean"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Chat completion, or text/event-stream when stream is true"
          },
          "401": {
            "description": "Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "402": {
            "description": "Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "502": {
            "description": "Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/account": {
      "get": {
        "summary": "Balance and totals",
        "security": [
          {
            "bearer": []
          }
        ],
        "responses": {
          "200": {
            "description": "Account",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Account"
                }
              }
            }
          },
          "401": {
            "description": "Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/usage": {
      "get": {
        "summary": "Recent requests with cost",
        "security": [
          {
            "bearer": []
          }
        ],
        "parameters": [
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "maximum": 500
            }
          }
        ],
        "responses": {
          "200": {
            "description": "List"
          },
          "401": {
            "description": "Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/topup": {
      "post": {
        "summary": "Add credit with USDC over x402",
        "description": "Without PAYMENT-SIGNATURE: 402 with a PAYMENT-REQUIRED header. With a valid PAYMENT-SIGNATURE: the transfer settles and the amount is added to the balance.",
        "security": [
          {
            "bearer": []
          }
        ],
        "parameters": [
          {
            "name": "amount",
            "in": "query",
            "schema": {
              "type": "string",
              "default": "1.00"
            },
            "description": "USD, 0.10 to 500.00"
          },
          {
            "name": "PAYMENT-SIGNATURE",
            "in": "header",
            "schema": {
              "type": "string"
            },
            "description": "x402 v2 payment payload, base64"
          }
        ],
        "responses": {
          "200": {
            "description": "Credited; PAYMENT-RESPONSE header holds the settlement"
          },
          "400": {
            "description": "Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "402": {
            "description": "Payment required; see the PAYMENT-REQUIRED header"
          }
        }
      }
    },
    "/topups": {
      "get": {
        "summary": "Top-up history",
        "security": [
          {
            "bearer": []
          }
        ],
        "responses": {
          "200": {
            "description": "List"
          },
          "401": {
            "description": "Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/keys": {
      "get": {
        "summary": "List API keys",
        "security": [
          {
            "bearer": []
          }
        ],
        "responses": {
          "200": {
            "description": "List"
          }
        }
      },
      "post": {
        "summary": "Create an API key",
        "security": [
          {
            "bearer": []
          }
        ],
        "responses": {
          "201": {
            "description": "Created; api_key shown once"
          }
        }
      }
    },
    "/keys/{id}": {
      "delete": {
        "summary": "Revoke an API key",
        "security": [
          {
            "bearer": []
          }
        ],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Revoked"
          },
          "404": {
            "description": "Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    }
  }
}
