{
  "openapi": "3.1.0",
  "info": {
    "title": "PeerFoundry Run",
    "version": "0.1.0",
    "summary": "Run python or javascript. No outbound network."
  },
  "servers": [
    {
      "url": "https://run.peerfoundry.ai"
    }
  ],
  "components": {
    "securitySchemes": {
      "bearer": {
        "type": "http",
        "scheme": "bearer",
        "description": "pf_ key."
      },
      "callerKey": {
        "type": "apiKey",
        "in": "header",
        "name": "X-Caller-Key"
      }
    },
    "schemas": {
      "runRequest": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "language",
          "code"
        ],
        "properties": {
          "language": {
            "type": "string",
            "enum": [
              "python",
              "javascript"
            ]
          },
          "code": {
            "type": "string"
          },
          "stdin": {
            "type": "string"
          },
          "files": {
            "type": "array",
            "items": {
              "type": "object",
              "additionalProperties": false,
              "required": [
                "name",
                "content"
              ],
              "properties": {
                "name": {
                  "type": "string"
                },
                "content": {
                  "type": "string"
                },
                "encoding": {
                  "type": "string",
                  "enum": [
                    "utf8",
                    "base64"
                  ]
                }
              }
            }
          },
          "timeout_s": {
            "const": 30
          },
          "caller_key": {
            "type": "string",
            "description": "Used when Authorization and X-Caller-Key are absent."
          }
        }
      },
      "runOk": {
        "type": "object",
        "required": [
          "run_id",
          "status",
          "exit_code",
          "stdout",
          "stderr",
          "duration_ms"
        ],
        "properties": {
          "run_id": {
            "type": "string"
          },
          "status": {
            "type": "string",
            "enum": [
              "ok",
              "exit_nonzero",
              "timeout",
              "infra_error"
            ]
          },
          "reason": {
            "type": "string",
            "enum": [
              "wall_clock_30s"
            ],
            "description": "Present when status is timeout. The 30 second wall clock sets wall_clock_30s."
          },
          "exit_code": {
            "type": [
              "integer",
              "null"
            ]
          },
          "stdout": {
            "type": "string"
          },
          "stderr": {
            "type": "string"
          },
          "stdout_truncated": {
            "type": "boolean"
          },
          "stderr_truncated": {
            "type": "boolean"
          },
          "files": {
            "type": "array"
          },
          "files_truncated": {
            "type": "boolean"
          },
          "duration_ms": {
            "type": "integer"
          },
          "language": {
            "type": "string",
            "enum": [
              "python",
              "javascript"
            ]
          }
        }
      },
      "rejected": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "error"
        ],
        "properties": {
          "error": {
            "type": "string"
          },
          "status": {
            "const": "rejected"
          },
          "run_id": {
            "type": "string"
          },
          "reason": {
            "type": "string"
          },
          "message": {
            "type": "string"
          },
          "next": {
            "type": "string"
          }
        }
      }
    }
  },
  "paths": {
    "/v1/keys": {
      "post": {
        "operationId": "mintKey",
        "summary": "Mint one pf_ key.",
        "requestBody": {
          "required": false,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "additionalProperties": false,
                "properties": {}
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "The raw key is shown once.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "ok",
                    "caller_key",
                    "key_prefix"
                  ],
                  "properties": {
                    "ok": {
                      "const": true
                    },
                    "caller_key": {
                      "type": "string"
                    },
                    "key_prefix": {
                      "type": "string"
                    }
                  }
                },
                "example": {
                  "ok": true,
                  "caller_key": "pf_0123456789abcdef0123456789abcdef",
                  "key_prefix": "pf_01234"
                }
              }
            }
          },
          "400": {
            "description": "The JSON is not valid, or the body asks for more than one key.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "error"
                  ],
                  "properties": {
                    "error": {
                      "type": "string",
                      "enum": [
                        "bad_json",
                        "bulk_rejected"
                      ]
                    }
                  }
                },
                "examples": {
                  "bad_json": {
                    "value": {
                      "error": "bad_json"
                    }
                  },
                  "bulk_rejected": {
                    "value": {
                      "error": "bulk_rejected"
                    }
                  }
                }
              }
            }
          },
          "403": {
            "description": "The region is blocked, or the network is Tor.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "error"
                  ],
                  "properties": {
                    "error": {
                      "type": "string",
                      "enum": [
                        "region_blocked",
                        "tor_refused"
                      ]
                    }
                  }
                },
                "examples": {
                  "region_blocked": {
                    "value": {
                      "error": "region_blocked"
                    }
                  },
                  "tor_refused": {
                    "value": {
                      "error": "tor_refused"
                    }
                  }
                }
              }
            }
          },
          "429": {
            "description": "A mint cap was hit. Wait for Retry-After.",
            "headers": {
              "Retry-After": {
                "description": "Seconds to wait before the next call.",
                "schema": {
                  "type": "integer",
                  "minimum": 0
                },
                "required": true
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "error"
                  ],
                  "properties": {
                    "error": {
                      "type": "string",
                      "enum": [
                        "rate_limited",
                        "mint_ip_cap",
                        "rate_limited_48",
                        "mint_48_cap",
                        "mint_global_hour",
                        "mint_global_cap"
                      ]
                    }
                  }
                },
                "examples": {
                  "rate_limited": {
                    "value": {
                      "error": "rate_limited"
                    }
                  },
                  "rate_limited_48": {
                    "value": {
                      "error": "rate_limited_48"
                    }
                  },
                  "mint_ip_cap": {
                    "value": {
                      "error": "mint_ip_cap"
                    }
                  },
                  "mint_48_cap": {
                    "value": {
                      "error": "mint_48_cap"
                    }
                  },
                  "mint_global_hour": {
                    "value": {
                      "error": "mint_global_hour"
                    }
                  },
                  "mint_global_cap": {
                    "value": {
                      "error": "mint_global_cap"
                    }
                  }
                }
              }
            }
          },
          "503": {
            "description": "Mint is off.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "error"
                  ],
                  "properties": {
                    "error": {
                      "type": "string"
                    },
                    "status": {
                      "const": "rejected"
                    },
                    "run_id": {
                      "type": "string"
                    },
                    "reason": {
                      "type": "string"
                    },
                    "message": {
                      "type": "string"
                    },
                    "next": {
                      "type": "string"
                    }
                  }
                },
                "examples": {
                  "free_disabled": {
                    "value": {
                      "error": "free_disabled"
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/v1/run": {
      "post": {
        "operationId": "runCode",
        "summary": "Run one program and return its output.",
        "description": "Send the pf_ key as Authorization Bearer, then X-Caller-Key, then caller_key. Call keys.create once. Pass caller_key from keys.create on every later tools/call. Authorization: Bearer pf_... or the header X-Caller-Key also works. A header key is used when one is present.",
        "security": [
          {
            "bearer": []
          },
          {
            "callerKey": []
          },
          {}
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "additionalProperties": false,
                "required": [
                  "language",
                  "code"
                ],
                "properties": {
                  "language": {
                    "type": "string",
                    "enum": [
                      "python",
                      "javascript"
                    ]
                  },
                  "code": {
                    "type": "string"
                  },
                  "stdin": {
                    "type": "string"
                  },
                  "files": {
                    "type": "array",
                    "items": {
                      "type": "object",
                      "additionalProperties": false,
                      "required": [
                        "name",
                        "content"
                      ],
                      "properties": {
                        "name": {
                          "type": "string"
                        },
                        "content": {
                          "type": "string"
                        },
                        "encoding": {
                          "type": "string",
                          "enum": [
                            "utf8",
                            "base64"
                          ]
                        }
                      }
                    }
                  },
                  "timeout_s": {
                    "const": 30
                  },
                  "caller_key": {
                    "type": "string",
                    "description": "Used when Authorization and X-Caller-Key are absent."
                  }
                }
              },
              "example": {
                "language": "python",
                "code": "print(1)"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "The run finished, exited non-zero, or hit the 30 second wall clock.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "run_id",
                    "status",
                    "exit_code",
                    "stdout",
                    "stderr",
                    "duration_ms"
                  ],
                  "properties": {
                    "run_id": {
                      "type": "string"
                    },
                    "status": {
                      "type": "string",
                      "enum": [
                        "ok",
                        "exit_nonzero",
                        "timeout",
                        "infra_error"
                      ]
                    },
                    "reason": {
                      "type": "string",
                      "enum": [
                        "wall_clock_30s"
                      ],
                      "description": "Present when status is timeout. The 30 second wall clock sets wall_clock_30s."
                    },
                    "exit_code": {
                      "type": [
                        "integer",
                        "null"
                      ]
                    },
                    "stdout": {
                      "type": "string"
                    },
                    "stderr": {
                      "type": "string"
                    },
                    "stdout_truncated": {
                      "type": "boolean"
                    },
                    "stderr_truncated": {
                      "type": "boolean"
                    },
                    "files": {
                      "type": "array"
                    },
                    "files_truncated": {
                      "type": "boolean"
                    },
                    "duration_ms": {
                      "type": "integer"
                    },
                    "language": {
                      "type": "string",
                      "enum": [
                        "python",
                        "javascript"
                      ]
                    }
                  }
                },
                "examples": {
                  "ok": {
                    "value": {
                      "run_id": "00000000-0000-4000-8000-000000000001",
                      "status": "ok",
                      "exit_code": 0,
                      "stdout": "1\n",
                      "stderr": "",
                      "duration_ms": 12,
                      "language": "python"
                    }
                  },
                  "timeout": {
                    "value": {
                      "run_id": "00000000-0000-4000-8000-000000000002",
                      "status": "timeout",
                      "reason": "wall_clock_30s",
                      "exit_code": null,
                      "stdout": "",
                      "stderr": "",
                      "duration_ms": 30000,
                      "language": "python"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "The JSON or a field is not valid.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "error"
                  ],
                  "properties": {
                    "error": {
                      "type": "string"
                    },
                    "status": {
                      "const": "rejected"
                    },
                    "run_id": {
                      "type": "string"
                    },
                    "reason": {
                      "type": "string"
                    },
                    "message": {
                      "type": "string"
                    },
                    "next": {
                      "type": "string"
                    }
                  }
                },
                "examples": {
                  "bad_json": {
                    "value": {
                      "error": "bad_json",
                      "status": "rejected",
                      "run_id": "00000000-0000-4000-8000-000000000010"
                    }
                  },
                  "bad_language": {
                    "value": {
                      "error": "bad_language",
                      "status": "rejected",
                      "run_id": "00000000-0000-4000-8000-000000000011"
                    }
                  },
                  "bad_code": {
                    "value": {
                      "error": "bad_code",
                      "status": "rejected",
                      "run_id": "00000000-0000-4000-8000-000000000012"
                    }
                  },
                  "bad_stdin": {
                    "value": {
                      "error": "bad_stdin",
                      "status": "rejected",
                      "run_id": "00000000-0000-4000-8000-000000000013"
                    }
                  },
                  "bad_timeout": {
                    "value": {
                      "error": "bad_timeout",
                      "status": "rejected",
                      "run_id": "00000000-0000-4000-8000-000000000014"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "The key is missing, unknown, revoked, or turned off.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "error"
                  ],
                  "properties": {
                    "error": {
                      "type": "string"
                    },
                    "status": {
                      "const": "rejected"
                    },
                    "run_id": {
                      "type": "string"
                    },
                    "reason": {
                      "type": "string"
                    },
                    "message": {
                      "type": "string"
                    },
                    "next": {
                      "type": "string"
                    }
                  }
                },
                "examples": {
                  "caller_key_required": {
                    "value": {
                      "error": "caller_key_required",
                      "status": "rejected",
                      "run_id": "00000000-0000-4000-8000-000000000003",
                      "message": "A caller key is required. Call keys.create once. Pass caller_key from keys.create on every later tools/call. Authorization: Bearer pf_... or the header X-Caller-Key also works. A header key is used when one is present.",
                      "next": "Call keys.create once. Pass caller_key from keys.create on every later tools/call. Authorization: Bearer pf_... or the header X-Caller-Key also works. A header key is used when one is present."
                    }
                  },
                  "bad_caller_key": {
                    "value": {
                      "error": "bad_caller_key",
                      "status": "rejected",
                      "run_id": "00000000-0000-4000-8000-000000000004"
                    }
                  }
                }
              }
            }
          },
          "403": {
            "description": "The region is blocked, the network is Tor, the key is suspended, or the input is refused.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "error"
                  ],
                  "properties": {
                    "error": {
                      "type": "string"
                    },
                    "status": {
                      "const": "rejected"
                    },
                    "run_id": {
                      "type": "string"
                    },
                    "reason": {
                      "type": "string"
                    },
                    "message": {
                      "type": "string"
                    },
                    "next": {
                      "type": "string"
                    }
                  }
                },
                "examples": {
                  "region_blocked": {
                    "value": {
                      "error": "region_blocked",
                      "status": "rejected",
                      "run_id": "00000000-0000-4000-8000-000000000005"
                    }
                  },
                  "tor_refused": {
                    "value": {
                      "error": "tor_refused",
                      "status": "rejected",
                      "run_id": "00000000-0000-4000-8000-000000000006"
                    }
                  },
                  "abuse_miner": {
                    "value": {
                      "error": "abuse_miner",
                      "status": "rejected",
                      "run_id": "00000000-0000-4000-8000-000000000015"
                    }
                  },
                  "abuse_fork": {
                    "value": {
                      "error": "abuse_fork",
                      "status": "rejected",
                      "run_id": "00000000-0000-4000-8000-000000000016"
                    }
                  },
                  "key_suspended": {
                    "value": {
                      "error": "key_suspended",
                      "status": "rejected",
                      "run_id": "00000000-0000-4000-8000-000000000017"
                    }
                  },
                  "ip_blocked": {
                    "value": {
                      "error": "ip_blocked",
                      "status": "rejected",
                      "run_id": "00000000-0000-4000-8000-000000000018"
                    }
                  }
                }
              }
            }
          },
          "413": {
            "description": "The body, code, stdin, or a file is over its cap.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "error"
                  ],
                  "properties": {
                    "error": {
                      "type": "string"
                    },
                    "status": {
                      "const": "rejected"
                    },
                    "run_id": {
                      "type": "string"
                    },
                    "reason": {
                      "type": "string"
                    },
                    "message": {
                      "type": "string"
                    },
                    "next": {
                      "type": "string"
                    }
                  }
                },
                "examples": {
                  "stdin": {
                    "value": {
                      "error": "too_large",
                      "reason": "stdin",
                      "status": "rejected",
                      "run_id": "00000000-0000-4000-8000-000000000019"
                    }
                  },
                  "code": {
                    "value": {
                      "error": "too_large",
                      "reason": "code",
                      "status": "rejected",
                      "run_id": "00000000-0000-4000-8000-00000000001a"
                    }
                  },
                  "body": {
                    "value": {
                      "error": "too_large",
                      "reason": "body"
                    }
                  }
                }
              }
            }
          },
          "429": {
            "description": "A run cap was hit. Wait for Retry-After.",
            "headers": {
              "Retry-After": {
                "description": "Seconds to wait before the next call.",
                "schema": {
                  "type": "integer",
                  "minimum": 0
                },
                "required": true
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "error"
                  ],
                  "properties": {
                    "error": {
                      "type": "string"
                    },
                    "status": {
                      "const": "rejected"
                    },
                    "run_id": {
                      "type": "string"
                    },
                    "reason": {
                      "type": "string"
                    },
                    "message": {
                      "type": "string"
                    },
                    "next": {
                      "type": "string"
                    }
                  }
                },
                "example": {
                  "error": "cap",
                  "reason": "runs_per_minute",
                  "status": "rejected",
                  "run_id": "00000000-0000-4000-8000-000000000007"
                }
              }
            }
          },
          "500": {
            "description": "The container run failed.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "run_id",
                    "status",
                    "exit_code",
                    "stdout",
                    "stderr",
                    "duration_ms"
                  ],
                  "properties": {
                    "run_id": {
                      "type": "string"
                    },
                    "status": {
                      "type": "string",
                      "enum": [
                        "ok",
                        "exit_nonzero",
                        "timeout",
                        "infra_error"
                      ]
                    },
                    "reason": {
                      "type": "string",
                      "enum": [
                        "wall_clock_30s"
                      ],
                      "description": "Present when status is timeout. The 30 second wall clock sets wall_clock_30s."
                    },
                    "exit_code": {
                      "type": [
                        "integer",
                        "null"
                      ]
                    },
                    "stdout": {
                      "type": "string"
                    },
                    "stderr": {
                      "type": "string"
                    },
                    "stdout_truncated": {
                      "type": "boolean"
                    },
                    "stderr_truncated": {
                      "type": "boolean"
                    },
                    "files": {
                      "type": "array"
                    },
                    "files_truncated": {
                      "type": "boolean"
                    },
                    "duration_ms": {
                      "type": "integer"
                    },
                    "language": {
                      "type": "string",
                      "enum": [
                        "python",
                        "javascript"
                      ]
                    }
                  }
                },
                "example": {
                  "run_id": "00000000-0000-4000-8000-00000000001b",
                  "status": "infra_error",
                  "exit_code": null,
                  "stdout": "",
                  "stderr": "",
                  "duration_ms": 45000,
                  "language": "python"
                }
              }
            }
          },
          "503": {
            "description": "The free tier is off, or a required check failed.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "error"
                  ],
                  "properties": {
                    "error": {
                      "type": "string"
                    },
                    "status": {
                      "const": "rejected"
                    },
                    "run_id": {
                      "type": "string"
                    },
                    "reason": {
                      "type": "string"
                    },
                    "message": {
                      "type": "string"
                    },
                    "next": {
                      "type": "string"
                    }
                  }
                },
                "examples": {
                  "free_disabled": {
                    "value": {
                      "error": "free_disabled",
                      "status": "rejected",
                      "run_id": "00000000-0000-4000-8000-000000000008"
                    }
                  },
                  "misconfigured": {
                    "value": {
                      "error": "misconfigured",
                      "status": "rejected",
                      "run_id": "00000000-0000-4000-8000-000000000009"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
