{
  "openapi": "3.1.0",
  "info": {
    "title": "Admire API",
    "description": "Upload records into an artifact source from a script, such as a GitHub Actions workflow, with a robot account's API token.",
    "version": "1"
  },
  "servers": [
    {
      "url": "https://app.{region}.admire.ai/api",
      "variables": {
        "region": {
          "default": "REGION",
          "description": "Your organization's region: `us` (United States) or `ca` (Canada)."
        }
      }
    }
  ],
  "components": {
    "securitySchemes": {
      "apiToken": {
        "type": "http",
        "scheme": "bearer",
        "description": "A robot account's API token, starting `admire_`, created on the robot's page under API access."
      }
    },
    "schemas": {
      "Me": {
        "type": "object",
        "properties": {
          "robot": {
            "type": "string",
            "description": "The robot account's name.",
            "examples": [
              "GitHub weekly sync"
            ]
          },
          "organization": {
            "type": "string",
            "description": "The organization's name.",
            "examples": [
              "Acme Coaching"
            ]
          },
          "token": {
            "type": "string",
            "description": "The API token's name.",
            "examples": [
              "GitHub Actions"
            ]
          },
          "expires_at": {
            "anyOf": [
              {
                "type": "string",
                "format": "date-time"
              },
              {
                "type": "null"
              }
            ],
            "description": "When the API token stops working, or null for a token that never expires.",
            "examples": [
              "2027-02-25T17:00:00.000Z"
            ]
          }
        },
        "required": [
          "robot",
          "organization",
          "token",
          "expires_at"
        ],
        "additionalProperties": false
      },
      "Error": {
        "type": "object",
        "properties": {
          "message": {
            "type": "string",
            "description": "What went wrong.",
            "examples": [
              "API token turned off"
            ]
          },
          "hint": {
            "description": "More about the error, such as which field is wrong.",
            "type": "string"
          },
          "code": {
            "type": "string",
            "description": "A stable code for the kind of error.",
            "examples": [
              "BR401"
            ]
          },
          "details": {
            "description": "Detail on the error, as a JSON string. A 400 for a field lists each field and why under `errors`.",
            "type": "string"
          }
        },
        "required": [
          "message",
          "code"
        ],
        "additionalProperties": false
      },
      "GatewayError": {
        "type": "object",
        "properties": {
          "message": {
            "type": "string",
            "description": "Always `Unauthorized`.",
            "examples": [
              "Unauthorized"
            ]
          }
        },
        "required": [
          "message"
        ],
        "additionalProperties": false
      },
      "UploadResult": {
        "type": "object",
        "properties": {
          "ingested": {
            "type": "integer",
            "minimum": 0,
            "maximum": 1000,
            "description": "How many records were added or updated.",
            "examples": [
              2
            ]
          },
          "source_id": {
            "type": "string",
            "format": "uuid",
            "description": "The artifact source the records went to.",
            "examples": [
              "0b9f1c2e-5d4a-4f6b-9a3e-7c8d2e1f0a4b"
            ]
          }
        },
        "required": [
          "ingested",
          "source_id"
        ],
        "additionalProperties": false
      }
    }
  },
  "security": [
    {
      "apiToken": []
    }
  ],
  "tags": [
    {
      "name": "Robot account",
      "description": "The robot account an API token belongs to."
    },
    {
      "name": "Artifact sources",
      "description": "Records in the artifact sources shared with the robot account."
    }
  ],
  "paths": {
    "/v1/me": {
      "get": {
        "operationId": "getMe",
        "tags": [
          "Robot account"
        ],
        "summary": "Who am I",
        "description": "Answers with the robot account's and organization's names, and the API token's name and expiry. Needs nothing shared with the robot account, so it checks a token before anything else is set up.",
        "responses": {
          "200": {
            "description": "The API token works.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Me"
                }
              }
            }
          },
          "401": {
            "description": "The API token is missing, made up or expired, and the gateway refused it with only `message`; or it is unknown, turned off or expired, or its robot account is disabled.",
            "content": {
              "application/json": {
                "schema": {
                  "anyOf": [
                    {
                      "$ref": "#/components/schemas/Error"
                    },
                    {
                      "$ref": "#/components/schemas/GatewayError"
                    }
                  ]
                }
              }
            }
          }
        }
      }
    },
    "/v1/artifact_sources/{id}/records": {
      "post": {
        "operationId": "uploadRecords",
        "tags": [
          "Artifact sources"
        ],
        "summary": "Upload records",
        "description": "Adds records to an artifact source, or updates the ones whose Reference ID (`ref_id`) it already holds, so a second run with the same Reference IDs adds no rows. The robot account needs Can edit on the source.",
        "responses": {
          "200": {
            "description": "The records were written.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/UploadResult"
                }
              }
            }
          },
          "400": {
            "description": "The request is not valid: `message` says what is wrong and, for a field that fails the schema, `hint` names it and says why.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "The API token is missing, made up or expired, and the gateway refused it with only `message`; or it is unknown, turned off or expired, or its robot account is disabled.",
            "content": {
              "application/json": {
                "schema": {
                  "anyOf": [
                    {
                      "$ref": "#/components/schemas/Error"
                    },
                    {
                      "$ref": "#/components/schemas/GatewayError"
                    }
                  ]
                }
              }
            }
          },
          "403": {
            "description": "The robot account can only view this artifact source: uploading needs Can edit.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "No artifact source with this id is shared with the robot account.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "The artifact source is a question set's results table, which only its question set writes.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "413": {
            "description": "The request body is larger than 5 MiB.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "The batch would take the artifact source past the records its organization allows, so nothing was written.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        },
        "parameters": [
          {
            "in": "path",
            "name": "id",
            "schema": {
              "type": "string",
              "format": "uuid",
              "pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$"
            },
            "required": true,
            "description": "The artifact source's id."
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "records": {
                    "minItems": 1,
                    "maxItems": 1000,
                    "type": "array",
                    "items": {
                      "type": "object",
                      "properties": {
                        "ref_id": {
                          "type": "string",
                          "minLength": 1,
                          "description": "The record's Reference ID, its key in the system it comes from. It appears once per call; sending it again in a later call updates the row."
                        },
                        "raw_data": {
                          "type": "object",
                          "propertyNames": {
                            "type": "string"
                          },
                          "additionalProperties": {},
                          "description": "The record's fields, keyed by name."
                        }
                      },
                      "required": [
                        "ref_id",
                        "raw_data"
                      ]
                    }
                  }
                },
                "required": [
                  "records"
                ],
                "examples": [
                  {
                    "records": [
                      {
                        "ref_id": "pr-1042",
                        "raw_data": {
                          "title": "Fix login",
                          "author": "sam",
                          "merged": true
                        }
                      }
                    ]
                  }
                ]
              }
            }
          }
        }
      }
    }
  }
}
