DataPress logo
API Reference

Uploading Files

Version:  4.0
Updated:  14th July 2024

Adding a file

The file upload API uses multipart/form-data with a file attachment.

  • The dataset.updatedAt field will automatically be updated to the current time.
  • Other metadata fields can be edited through the PATCH API.
POST
https://data.website/api/dataset/:id/resources
Path Parameters
:id5-character ID of the dataset, eg. "ab12x"stringRequired
HTTP Headers
AuthorizationAPI key with editor permissions.stringRequired
Content-TypeMust be "multipart/form-data". Most clients will set this automatically.stringRequired
Form Fields
fileThe file to upload.fileRequired
titleThe title of the resource on the page.stringOptional
HTTP Return Codes
200Returns the full dataset JSON.
400Returns a JSON object explaining the error.
403API key does not have editor permissions.

Example: Upload a CSV file

The newly uploaded CSV is added to the top of the file list on the webpage.

import requests, os, json

dataset_id = "20abc"

url = f"https://data.website/api/dataset/{dataset_id}/resources"
headers = {
    "Authorization": os.getenv("DATAPRESS_API_KEY")
}

# Prepare the file and metadata
with open('example.csv', 'rb') as file:
    files = {
        'file': ('example.csv', file, 'text/csv')
    }
    # Optional metadata field
    data = { 'title': 'Monthly Spending Data' }
    # Requests will set Content-Type automatically
    response = requests.post(url, files=files, data=data, headers=headers)
print(json.dumps(response.json(), indent=2))

Replacing a file

To replace an existing file, use the same multipart/form-data format but include the resource ID in the URL path.

  • The dataset.updatedAt field will automatically be updated to the current time.
  • Other metadata fields can be edited through the PATCH API.
POST
https://data.website/api/dataset/:id/resources/:resource_id
Path Parameters
:id5-character ID of the dataset, eg. "ab12x"stringRequired
:resource_id3-character ID of the resource to replace, eg. "0ck"stringRequired
HTTP Headers
AuthorizationAPI key with editor permissions.stringRequired
Content-TypeMust be "multipart/form-data". Most clients will set this automatically.stringRequired
Form Fields
fileThe new file to upload.fileRequired
titleThe title of the resource on the page.stringOptional
HTTP Return Codes
200Returns the full dataset JSON.
400Returns a JSON object explaining the error.
403API key does not have editor permissions.

Example: Replace a CSV file

This overwrites the existing resource file and updates the title.

import requests, os, json

dataset_id = "20abc"
resource_id = "0ck"

url = f"https://data.website/api/dataset/{dataset_id}/resources/{resource_id}"
headers = {
    "Authorization": os.getenv("DATAPRESS_API_KEY")
}

# Prepare the replacement file
with open('updated.csv', 'rb') as file:
    files = {
        'file': ('updated.csv', file, 'text/csv')
    }
    # Optional metadata field
    data = { 'title': 'Monthly Spending Data (Updated)' }
    # Requests will set Content-Type automatically
    response = requests.post(url, files=files, data=data, headers=headers)
print(json.dumps(response.json(), indent=2))