Help

Save the date! Join us on October 16 for our Product Ops launch event. Register here.

Re: Where to start; using scripting block to POST to an api

Solved
Jump to Solution
2550 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Hemi_Phillips
6 - Interface Innovator
6 - Interface Innovator

Hello! this is my first time using the scripting block and first time trying to use an API with code (I’ve used lots of no-code tools in the past; Zapier, Integromat etc.)

I want to use a text field in my base to create a ‘tag’ in Toggl using the Toggl api;

The Toggl API documentation looks good, and I think I understand most of it, but I don’t know how to actually start - what is the first think I need to write?
All the toggl examples start with ‘curl’ e.g.

curl -v -u 1971800d4d82861d8f2c1651fea4d212:api_token \
	-H "Content-Type: application/json" \
	-d '{"time_entry":{"description":"New time entry","created_with":"API example code","start":"2012-02-12T15:35:47+02:00","duration":1200,"wid":31366}}' \
	 -X POST https://api.track.toggl.com/api/v8/time_entries

Is this what I would paste into the script editor?
I assume I need the toggle api url somewhere ( api.track.toggl.com) so the script knows where to get/post the data but I don’t know where or how to add this url.

Can someone please point me in the right direction?
Cheers!

1 Solution

Accepted Solutions
kuovonne
18 - Pluto
18 - Pluto

You have a syntax error in your body. This is not a fetch issue, but a JavaScript issue in dealing with strings.

The body should be a JSON string but your opening and closing quotes are messing up the string.

First assign the body to a variable before the fetch …

let body = {"tag":
  {
    "name":"Test-Tag",
    "wid":12345
  }
}

Then in the fetch …

body: JSON.stringify(body),

See Solution in Thread

6 Replies 6

You can start with fetch. See the documentation for more info on how Airtable implements fetch. You can also Google how to convert a curl request to a fetch request.

Hemi_Phillips
6 - Interface Innovator
6 - Interface Innovator

Thank so much for your help!
Using this tool (cURL to Fetch) it looks like this is the code I should be using:

fetch("https://api.track.toggl.com/api/v8/tags", {
  body: "{"tag":{"name":"Test-Tag","wid":12345}}",
  headers: {
    Authorization: "Basic abc123:api_token",
    "Content-Type": "application/json"
  },
  method: "POST"
});

However, I get this error:

SyntaxError: Unexpected identifier
on line 1
at o on line 1
at Generator._invoke on line 1
at Generator.F.forEach.u. [as next] on line 1
at u on line 1
at a on line 1
on line 1
on line 1

Looking at the airtable fetch documentation I can’t figure out what this error code is trying to say.

Any ideas?
Thanks!

kuovonne
18 - Pluto
18 - Pluto

You have a syntax error in your body. This is not a fetch issue, but a JavaScript issue in dealing with strings.

The body should be a JSON string but your opening and closing quotes are messing up the string.

First assign the body to a variable before the fetch …

let body = {"tag":
  {
    "name":"Test-Tag",
    "wid":12345
  }
}

Then in the fetch …

body: JSON.stringify(body),
Jeremy_Wilt
4 - Data Explorer
4 - Data Explorer

This was really helpful @kuovonne! I’m trying to do something similar: creating a user in Softr when an Airtable user gets created in my table.

I’m so close to getting this working based on your feedback, but I get back a 'Bad Request" that “User email field can’t be empty” even though I’m passing the email from airtable to softr in the script I’m using.

What am I doing wrong here? I can’t figure out how to structure the email field correctly.

let table = base.getTable("results");

let body = {"user":
  {
    "full_name": "John Richardson",
    "Email": "jrichardson@gmail.com",
    "password": "12345678",
    "generate_magic_link": "true",
  }
}

let response = await fetch('https://studio-api.softr.io/v1/api/users', {
    method: 'POST',
    body: JSON.stringify(body),
    headers: {
        'Content-Type': 'application/json', 
        'Softr-Api-Key': '----------',
        'Softr-Domain': '----.softr.app',
    },
});
console.log(await response.json());

Hi Jeremy,

The issue is you’ve got a typo. It should be email, not Email.

If you ever get tired of writing API/fetch code, check out Data Fetcher for a no-code way to do it.

Lol. That was my first guess, but as I wasn’t familiar with the api, I didn’t want to accidentally lead anyone astray.