File Uploads#
API version: v1alpha1
DoltHub provides an API endpoint for uploading data files into a database. Uploads run asynchronously — the same endpoint also lets you poll the import job for status.
Note: please send requests to
https://www.dolthub.com, nothttps://dolthub.com.
Upload a file#
Here is an example of uploading a file lacma.csv to create a table lacma on a database museum-collections using an authorization token. Note that the file import operation is asynchronous and creates an operation that can be polled to get the result.
To poll the operation and check its status, you can use the operationName in the returned response of the file upload post to query the API. Once the operation is complete, the response will contain a job_id field indicating the job that’s running the file import as well as the id of the pull request that’s created when the import job is completed.
Keep in mind that the time it takes for the import operation to complete can vary depending on the size of the file and the complexity of the changes being applied to the database. The file size limit is 100 MB.
Include this header in your request with the API token you created.
headers = {
'authorization': 'token [api token you created]'
}
To upload the file, include two fields in the request body, file and params, the file should be type of Blob, and params should be a JSON object.
/{owner}/{database}/upload
Upload a file to a DoltHub database
This endpoint allows you to upload a file to DoltHub to create, update, overwrite, or replace a table.
https://www.dolthub.com/api/v1alpha1/{owner}/{database}/uploadParameters
| Name | In | Type | Required | Description |
|---|---|---|---|---|
owner | path | string | Yes | The name of the database owner. Example: dolthub |
database | path | string | Yes | The name of the database. Example: museum-collections |
Request Body
Content-Type: multipart/form-data
| Field | Type | Required | Description |
|---|---|---|---|
file | string | No | The file to be uploaded. |
params | object | No |
Responses
Body — application/json
{
"status": "Success",
"database_owner": "dolthub",
"database_name": "museum-collections",
"branch_name": "main",
"table_name": "lacma",
"operation_name": "repositoryOwners/dolthub/repositories/museum-collections/jobs/b09a9221-9dcb-4a15-9ca8-a64656946f12",
"user_operation_name": "users/liuliu/userOperations/5e4834c9-375d-4bbd-bdaf-09eb0734127c"
}
| Field | Type | Description |
|---|---|---|
status | string | |
database_owner | string | |
database_name | string | |
branch_name | string | |
table_name | string | |
operation_name | string | The job id that is performing the upload. |
user_operation_name | string | The operation id that is associated to the upload job. It corresponds to the 'operation_name' field returned in the response of the list operations API. |
Body — application/json
{
"status": "Error",
"message": "Invalid request body",
"database_owner": "dolthub",
"database_name": "museum-collections",
"branch_name": "main",
"table_name": "lacma"
}
| Field | Type | Description |
|---|---|---|
status | string | |
message | string | |
database_owner | string | |
database_name | string | |
branch_name | string | |
table_name | string |
Then use GET to poll the operation to check if the import operation is done.
/{owner}/{database}/upload
Check import operation status
Poll the operation to check if the file import operation is done
https://www.dolthub.com/api/v1alpha1/{owner}/{database}/uploadParameters
| Name | In | Type | Required | Description |
|---|---|---|---|---|
owner | path | string | Yes | The owner of the database Example: dolthub |
database | path | string | Yes | The database name Example: museum-collections |
branch | query | string | Yes | The name of the branch to upload the file to. Example: main |
operationName | query | string | Yes | The operation name to check Example: repositoryOwners/dolthub/repositories/museum-collections/jobs/b09a9221-9dcb-4a15-9ca8-a64656946f12 |
Responses
Body — application/json
{
"status": "Success",
"operation_name": "repositoryOwners/dolthub/repositories/museum-collections/jobs/b09a9221-9dcb-4a15-9ca8-a64656946f12",
"job_created": true,
"database_owner": "dolthub",
"database_name": "museum-collections",
"pull_id": "66",
"job_status": "Completed"
}
| Field | Type | Description |
|---|---|---|
status | string | The status of the operation, Success if the file import was successful |
operation_name | string | The operation name |
job_created | boolean | True if the import job is created, False otherwise |
database_owner | string | The owner of the database |
database_name | string | The name of the database |
pull_id | string | The ID of the pull request that the import job created. This is only present if the import job is completed |
job_status | string | The status of the job, In Progress if the job is still running, Completed if the job is done, Pending if the job is waiting to be run |
Body — application/json
{
"status": "Error",
"message": "Error polling an operation status.",
"operation_name": "repositoryOwners/dolthub/repositories/museum-collections/jobs/b09a9221-9dcb-4a15-9ca8-a64656946f12"
}
| Field | Type | Description |
|---|---|---|
status | string | |
message | string | |
operation_name | string |
Here is an example of uploading a CSV file to create a table through this api endpoint in Javascript, you can reference the dolt table import documentation for additional information.:
Note Please make sure to send your requests to
https://www.dolthub.com/api/v1alpha1/{owner}/{database}/uploadinstead ofhttps://www.dolthub.com/api/v1alpha1/{owner}/{database}/upload/, do not need the last/.
const fs = require("fs");
const url =
"https://www.dolthub.com/api/v1alpha1/dolthub/museum-collections/upload";
const headers = {
"Content-Type": "application/json",
authorization: [api token you created],
};
const filePath = "lacma.csv";
fetchFileAndSend(filePath);
async function fetchFileAndSend(filePath) {
const params = {
tableName: "lacma",
fileName: "lacma.csv",
branchName:"main",
fileType: "Csv",
importOp: "Create",
primaryKeys: ["id"],
};
const formData = new FormData();
const fileData = fs.readFileSync(filePath);
const blob = new Blob([buffer], { type: "application/octet-stream" });
await formData.append("file", blob, "lacma.csv");
formData.append("params", JSON.stringify(params));
fetch(url, {
method: "POST",
headers,
body: formData,
})
.then((response) => {
// process response
})
.catch((error) => {
// process error
});
}
And an example of polling the job status in Javascript:
function pollOperation(op_name,branch_name) {
const url = `https: //www.dolthub.com/api/v1alpha1/dolthub/museum-collections/upload?branchName=${branch_name}&operationName=${op_name}`;
const headers = {
"Content-Type": "application/json",
authorization: [api token you created],
};
while (true) {
const res = await fetch(url, {
method: "GET",
headers,
});
const data = await res.json();
if (data.job_created) {
return data;
} else {
await new Promise(r => setTimeout(r, 1000));
}
}
}