Monday.com file upload api

I am sending my request to monday.com adding file I am getting a cors error for all other I’m using the mopnday js sdk and everything workds fine

async function uploadFile(file, ITEM_ID =“item_id”, COLUMN_ID = “files”) {
console.log(file, ITEM_ID, COLUMN_ID);
const query = mutation($file: File!) { add_file_to_column( file: $file, item_id: ${ITEM_ID}, column_id: "${COLUMN_ID}" ) { id } };

    const formData = new FormData();
    formData.append('query', query);
    formData.append('variables[file]', file);

    const response = await fetch('https://api.monday.com/v2/file', {
        method: 'POST',
        headers: {
            'Authorization': env.key
        },
        body: formData
    });

    if (!response.ok) {
        throw new Error('Network response was not ok');
    }

    return response.json();
}

Moreover when I copy the request to postman and send the same request from postman by copying it from my browser developer tools it works.

Update
Disabled cors in chrome using

open -n -a /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --args --user-data-dir="/tmp/chrome_dev_test" --disable-web-security

The same request now works Can you tell me how to work with cors in files api.

Is there a way to do it via the monday js sdk. Can onyone tell me

Hello there @cygnus,

Regarding your question, you can not directly upload a file using an SDK method but you can open a pop up for the user to do that as explained here :smile:

@Matias.Monday

Hey I am creating a quick item add functionality where I want to add an item quickly to another board how can I add file to another board doing it the way you provided user has to add files later on which is not good user experience. I am creating an Item like this

mutation {
create_item (board_id: ${boardId},
item_name: “${data.name}”,
column_values: “${data.mondayData}”),
{id}
}`

Any other way would be fine for me. I tried doing it in another way where I would handle the file columns separately after I get the response from the above mutation and use add file to column to add the file after getting response from create-item but this gives me cors error how can i remove the cors error

const query = mutation($file: File!) { add_file_to_column( file: $file, item_id: ${ITEM_ID}, column_id: "${COLUMN_ID}" ) { id } };

    const formData = new FormData();
    formData.append('query', query);
    formData.append('variables[file]', file);

    const response = await fetch('https://api.monday.com/v2/file', {
        method: 'POST',
        headers: {
            'Authorization': env.key,
        },
        body: formData
    });

Hello again @cygnus,

The add_file_to_column mutation would be the way to go here.

If you are having issues with it, please fill this form adding as much information as possible to it (such as account ID, board IDs, item IDs, timestamps, etc.) so that our team can take a look into it!