JavaScript syntax error in Zapier workflow: Unexpected closing bracket

I’m working on a Zapier automation that pulls information from a spreadsheet and processes it with JavaScript before sending it via email. However, I keep getting this error message:

SyntaxError: Unexpected token ]

Here’s my current code:

const payload = {
    "subscriber_lists": [
        "b1c40237-78e7-4933-bd17-cf87d4ff5881"
    ],
    "users": [
        {
            "email_address": "[email protected]",
            "given_name": "John",
            "properties": {
                "field_1": "value1",
                "field_2": "value2",
                "field_3": "value3"
            }
        }
    ]
}

const response = await fetch('https://api.sendgrid.com/v3/marketing/contacts', {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json',
        'authorization': 'Bearer <<token>>'
    },
    body: JSON.stringify(payload)
})

.then(res => res.json())
.then(result => {
    console.log('Request successful:', result);
})
.catch((err) => {
    console.error('Request failed:', err);
});

return { response };

I found this code in a tutorial and the author mentioned it worked fine for them. Can anyone help me figure out what’s causing this bracket error?

You’re mixing async/await with .then() chains on your fetch call - that’s what’s breaking it. When you use await fetch(), the response is already resolved, so adding .then() afterwards creates a conflict Zapier’s JavaScript environment can’t handle.

Pick one approach and stick with it. Try this instead: const response = await fetch(...); const result = await response.json(); and wrap it in a try/catch block for errors.

I’ve hit this same issue with Zapier’s Code actions before. Their JavaScript parser is way more strict about mixing these patterns than regular Node.js.

ya, i think u’re right. removing .then and sticking with async/await should help. something like const result = await response.json() would look cleaner too. zapier can be a bit picky sometimes, good luck!

This happens because Zapier’s JavaScript interpreter doesn’t handle mixed async patterns well. You’re mixing await with .then() chaining, which confuses the execution context - Zapier can’t figure out what ‘response’ actually contains when you return it. I’ve hit this same issue before. The return statement placement is probably throwing things off too. Ditch the mixed approach and go pure async/await. Store your final result in a variable first, then return that specific value instead of the whole response object. That way Zapier gets exactly what it expects for the next step.