{
  "openapi": "3.0.3",
  "info": {
    "title": "Table endpoints",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "https://api.dune.com/api"
    }
  ],
  "paths": {
    "/v1/table/create": {
      "post": {
        "tags": [
          "table"
        ],
        "summary": "Create a new Dune table",
        "description": "Create a new Dune table with the specified name and namespace.",
        "requestBody": {
          "description": "Create a new Dune Table for uploads",
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreateTableRequest"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "The Dune table `namespace.table_name` already existed with the same data as the request.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CreateTableResponse"
                }
              }
            }
          },
          "201": {
            "description": "The Dune table `namespace.table_name` was created successfully.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CreateTableResponse"
                }
              }
            }
          },
          "400": {
            "description": "The request was invalid. Please consult the error message.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string",
                      "example": "Content-Type header is not application/json"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "You are unauthorized to create this table. Either the table is private and you cannot create a private table, or you are not allowed to create a table under namespace `namespace`, or you are not allowed to create a table with name `table_name`.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string",
                      "example": "You are not authorized to create a table under the 'other_user' namespace"
                    }
                  }
                }
              }
            }
          },
          "409": {
            "description": "A table with this name already exists.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string",
                      "example": "This table already exists"
                    }
                  }
                }
              }
            }
          },
          "500": {
            "$ref": "#/components/responses/InternalError"
          }
        }
      }
    },
    "/v1/table/{namespace}/{table_name}/insert": {
      "post": {
        "tags": [
          "table"
        ],
        "summary": "Insert data into a Dune table.",
        "description": "Insert the data in a file into a table.",
        "parameters": [
          {
            "name": "namespace",
            "in": "path",
            "description": "The namespace of the table to insert into (e.g. `my_user`).",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "table_name",
            "in": "path",
            "description": "The name of the table to insert into (e.g. `interest_rates`).",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "description": "The file to insert into the table.",
          "content": {
            "text/csv": {},
            "application/x-ndjson": {}
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "Successful operation",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/InsertTableResponse"
                }
              }
            }
          },
          "400": {
            "description": "Bad request",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string",
                      "example": "Invalid content type 'text/plain'. We support CSV (Content-Type: text/csv) and newline delimited JSON (Content-Type: application/x-ndjson)."
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Unauthorized",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string",
                      "example": "You are not authorized to write to the table named 'other_user.interest_rates'"
                    }
                  }
                }
              }
            }
          },
          "404": {
            "$ref": "#/components/responses/TableNotFoundError"
          },
          "500": {
            "$ref": "#/components/responses/InternalError"
          }
        }
      }
    },
    "/v1/table/{namespace}/{table_name}/clear": {
      "post": {
        "tags": [
          "table"
        ],
        "summary": "Clear data in a table.",
        "description": "Clear data in a table.",
        "parameters": [
          {
            "name": "namespace",
            "in": "path",
            "description": "The the table to clear (e.g. `my_user`).",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "table_name",
            "in": "path",
            "description": "The name of the table to clear (e.g. `interest_rates`).",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Successful operation",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ClearTableResponse"
                }
              }
            }
          },
          "401": {
            "description": "Unauthorized",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string",
                      "example": "You are not authorized to write to namespace 'other_user'. You are allowed to upload into the namespace which is the same as the user or team which your token is associated with."
                    }
                  }
                }
              }
            }
          },
          "404": {
            "$ref": "#/components/responses/TableNotFoundError"
          },
          "500": {
            "$ref": "#/components/responses/InternalError"
          }
        }
      }
    },
    "/v1/table/{namespace}/{table_name}": {
      "delete": {
        "tags": [
          "table"
        ],
        "summary": "Delete a Dune table",
        "description": "Delete a Dune table with the specified name and namespace.",
        "parameters": [
          {
            "name": "namespace",
            "in": "path",
            "description": "The namespace of the table to delete (e.g. `my_user`).",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "table_name",
            "in": "path",
            "description": "The name of the table to delete (e.g. `interest_rates`).",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Table successfully deleted",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/DeleteTableResponse"
                }
              }
            }
          },
          "401": {
            "description": "Unauthorized",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "description": "The error message",
                      "type": "string",
                      "example": "You are not authorized to delete table other_user.interest_rates. If your key belongs to the correct namespace, you need admin privileges to delete."
                    }
                  }
                }
              }
            }
          },
          "404": {
            "description": "Table not found",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string",
                      "example": "Table my_user.interest_rates not found"
                    }
                  }
                }
              }
            }
          },
          "500": {
            "$ref": "#/components/responses/InternalError"
          }
        }
      }
    },
    "/v1/blockchain/{chain}/{table_name}/insert": {
      "post": {
        "tags": [
          "table"
        ],
        "summary": "Insert data into a blockchain table.",
        "description": "Insert blockchain data into supported table with predefined schema",
        "parameters": [
          {
            "name": "chain",
            "in": "path",
            "description": "The blockchain of the table to insert into (e.g. `my_chain`).",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "table_name",
            "in": "path",
            "description": "The name of one of the supported tables to insert into (e.g. `blocks`).",
            "required": true,
            "schema": {
              "type": "string",
              "enum": [
                "blocks",
                "transactions",
                "logs",
                "traces"
              ]
            }
          }
        ],
        "requestBody": {
          "description": "The file to insert into the table.",
          "content": {
            "text/csv": {},
            "application/x-ndjson": {}
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "Successful operation",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/InsertTableResponse"
                }
              }
            }
          },
          "400": {
            "description": "Bad request",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string",
                      "example": "Invalid content type 'text/plain'. We support CSV (Content-Type: text/csv) and newline delimited JSON (Content-Type: application/x-ndjson)."
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Unauthorized",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string",
                      "example": "You are not authorized to create a table under the 'my_chain' namespace. Your accessible namespaces: 'other_namespace'"
                    }
                  }
                }
              }
            }
          },
          "500": {
            "$ref": "#/components/responses/InternalError"
          }
        }
      }
    },
    "/v1/blockchain/{chain}/{table_name}/delete": {
      "post": {
        "tags": [
          "table"
        ],
        "summary": "Deletes data from a predicate in a blockchain table. Other data remains intact.",
        "description": "Deletes data from a predicate in a blockchain table.",
        "parameters": [
          {
            "name": "chain",
            "in": "path",
            "description": "The blockchain of the table to clear (e.g. `my_chain`).",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "table_name",
            "in": "path",
            "description": "The name of one of the supported tables to clear (e.g. `blocks`).",
            "required": true,
            "schema": {
              "type": "string",
              "enum": [
                "blocks",
                "transactions",
                "logs",
                "traces"
              ]
            }
          },
          {
            "name": "date",
            "in": "query",
            "description": "The date to delete data from in the format `YYYY-MM-DD` (e.g. `2024-04-28`).",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Successful operation",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/DeletePredicateResponse"
                }
              }
            }
          },
          "400": {
            "description": "Bad request",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string",
                      "example": "Table name other_table is not supported for blockchain endpoint"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Unauthorized",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string",
                      "example": "You are not authorized to delete in namespace 'other_chain'. You are allowed to delete from the namespace which is the same as the user or team which your token is associated with."
                    }
                  }
                }
              }
            }
          },
          "500": {
            "$ref": "#/components/responses/InternalError"
          }
        }
      }
    },
    "/v1/blockchain/{chain}/{table_name}/clear": {
      "post": {
        "tags": [
          "table"
        ],
        "summary": "Clear data in a blockchain table.",
        "description": "Clear data in a blockchain table.",
        "parameters": [
          {
            "name": "chain",
            "in": "path",
            "description": "The blockchain of the table to clear (e.g. `my_chain`).",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "table_name",
            "in": "path",
            "description": "The name of one of the supported tables to clear (e.g. `blocks`).",
            "required": true,
            "schema": {
              "type": "string",
              "enum": [
                "blocks",
                "transactions",
                "logs",
                "traces"
              ]
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Successful operation",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ClearTableResponse"
                }
              }
            }
          },
          "400": {
            "description": "Bad request",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string",
                      "example": "Table name other_table is not supported for blockchain endpoint"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Unauthorized",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string",
                      "example": "You are not authorized to write to namespace 'other_chain'. You are allowed to upload into the namespace which is the same as the user or team which your token is associated with."
                    }
                  }
                }
              }
            }
          },
          "500": {
            "$ref": "#/components/responses/InternalError"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "CreateTableRequest": {
        "required": [
          "table_name",
          "schema"
        ],
        "type": "object",
        "properties": {
          "namespace": {
            "description": "The namespace of the table to create. Only the name of your associated API key is allowed at the moment, i.e. either `my_user` or `my_team`. (Optional. Default is the namespace of your API key.)",
            "type": "string",
            "example": "my_user"
          },
          "table_name": {
            "description": "The name of the table to create. Must begin with a lowercase letter and contain only lowercase letters, digits, and underscores.",
            "type": "string",
            "example": "my_data"
          },
          "schema": {
            "description": "An ordered list of columns that define the table schema. Cannot be empty.",
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Column"
            }
          },
          "is_private": {
            "type": "boolean",
            "default": false,
            "description": "If true, the table will be private. If private it is only visible to the team or user that your API key is associated with."
          },
          "description": {
            "type": "string",
            "description": "A description of the table."
          }
        }
      },
      "Column": {
        "description": "A column of the table.",
        "required": [
          "name",
          "type"
        ],
        "properties": {
          "name": {
            "type": "string",
            "description": "The column name. Can contain letters, numbers, and underscores, but must begin with a letter or an underscore.",
            "example": "my_column123"
          },
          "type": {
            "type": "string",
            "enum": [
              "varchar",
              "integer",
              "double",
              "boolean",
              "int256",
              "uint256",
              "varbinary",
              "timestamp"
            ],
            "description": "The column type.",
            "example": "timestamp"
          },
          "nullable": {
            "type": "boolean",
            "description": "Flag that denotes if the column is nullable (true/false, true by default).",
            "example": false
          }
        }
      },
      "CreateTableResponse": {
        "type": "object",
        "properties": {
          "namespace": {
            "description": "The namespace of the table.",
            "type": "string",
            "example": "my_user"
          },
          "table_name": {
            "description": "The name of the table.",
            "type": "string",
            "example": "my_data"
          },
          "full_name": {
            "description": "The full name of the table, as it should be referred to in a query.",
            "type": "string",
            "example": "dune.my_user.my_data"
          },
          "example_query": {
            "description": "An example query to use on Dune querying your new table.",
            "type": "string",
            "example": "select * from dune.my_user.my_data"
          },
          "already_existed": {
            "description": "Whether the request already existed.",
            "type": "boolean",
            "example": false
          },
          "message": {
            "description": "A message explaining whether the table was created in this request or not. The same information is encoded in `already_existed`.",
            "type": "string",
            "example": "Table created successfully"
          }
        }
      },
      "InsertTableResponse": {
        "type": "object",
        "properties": {
          "rows_written": {
            "description": "The number of rows that were written to the table.",
            "type": "number",
            "example": 9000
          },
          "bytes_written": {
            "description": "The number of bytes that were written to the table.",
            "type": "number",
            "example": 90
          }
        }
      },
      "ClearTableResponse": {
        "type": "object",
        "properties": {
          "message": {
            "description": "The confirmation message of the cleared table",
            "type": "string",
            "example": "Table my_user.interest_rates successfully cleared"
          }
        }
      },
      "DeletePredicateResponse": {
        "type": "object",
        "properties": {
          "message": {
            "description": "The confirmation message of the deleted predicate",
            "type": "string",
            "example": "Successfully deleted from table namespace.logs"
          },
          "num_files_removed": {
            "description": "The number of files removed from the underlying filesystem",
            "type": "number",
            "example": 1
          },
          "num_rows_deleted": {
            "description": "The number of rows deleted from the specified table",
            "type": "number",
            "example": 1925
          }
        }
      },
      "DeleteTableResponse": {
        "type": "object",
        "properties": {
          "message": {
            "description": "The confirmation message of the deleted table",
            "type": "string",
            "example": "Table my_user.interest_rates successfully deleted"
          }
        }
      },
      "BlockchainTablesSchemas": {
        "type": "object",
        "properties": {
          "blocks": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Column"
            }
          }
        }
      }
    },
    "securitySchemes": {
      "api_key": {
        "type": "apiKey",
        "in": "header",
        "name": "x-dune-api-key",
        "description": "The API key of your team or user."
      }
    },
    "responses": {
      "TableNotFoundError": {
        "description": "The table does not exist",
        "content": {
          "application/json": {
            "schema": {
              "type": "object",
              "properties": {
                "error": {
                  "type": "string",
                  "example": "This table was not found"
                }
              }
            }
          }
        }
      },
      "InternalError": {
        "description": "Internal error occurred",
        "content": {
          "application/json": {
            "schema": {
              "type": "object",
              "properties": {
                "error": {
                  "type": "string",
                  "example": "Internal server error"
                }
              }
            }
          }
        }
      }
    }
  },
  "security": [
    {
      "api_key": []
    }
  ]
}