Help needed uploading Data to Hugging Face Dataset via Apps Script

Hi everyone,

I’m trying to upload data to my Hugging Face dataset using the following Apps Script, but it seems to be giving me an error. Can anyone confirm if the API URL I’m using is correct? Or is there another API endpoint I should use to upload a small file to my dataset?

Any feedback on what I’m doing wrong or how to correctly use the API to upload a file to my Hugging Face dataset would be much appreciated!

Thank you in advance!

Here’s the Apps Script I am using:

function pushDataToHuggingFace() {
const token = ‘[my Token]’;
const datasetRepo = ‘[user]/[my dataset name]’;

// 1. Get data from Google Sheets
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(‘Keywords’);
const data = sheet.getRange(‘A2:B11’).getValues();

// 2. Convert data to valid JSONL format
const jsonlContent = data.map(row =>
JSON.stringify({ include: row[0], exclude: row[1] })
).join(‘\n’);

// 3. Set up request with the updated API URL
const url = https://huggingface.co/api/datasets/${datasetRepo}/commit/main;
const options = {
method: ‘PUT’,
headers: {
‘Authorization’: Bearer ${token},
‘Content-Type’: ‘application/json’
},
payload: JSON.stringify({
files: [{
path: ‘data/datos.jsonl’,
content: Utilities.base64Encode(jsonlContent),
encoding: ‘base64’
}],
commit_message: ‘Update from Google Sheets’,
repo_type: ‘dataset’
}),
muteHttpExceptions: true
};

// 4. Execute and handle response
try {
const response = UrlFetchApp.fetch(url, options);
const code = response.getResponseCode();

if (code === 200 || code === 201) {
  Logger.log(`✅ Success! Verify here: https://huggingface.co/datasets/${datasetRepo}/tree/main/data`);
} else {
  Logger.log(`❌ Error ${code}: ${response.getContentText()}`);
}

} catch (error) {
Logger.log(🔥 Error: ${error.message});
}
}

1 Like

Since errors are likely to occur with normal web requests, it is easier to use libraries.

1 Like