Skip to main content
Solved

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

  • August 17, 2021
  • 6 replies
  • 90 views

Forum|alt.badge.img+6

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!

Best answer by kuovonne

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),

6 replies

kuovonne
Forum|alt.badge.img+29
  • Brainy
  • 6009 replies
  • August 17, 2021

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.


Forum|alt.badge.img+6
  • Author
  • Participating Frequently
  • 9 replies
  • August 20, 2021

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
Forum|alt.badge.img+29
  • Brainy
  • 6009 replies
  • Answer
  • August 20, 2021

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),

  • New Participant
  • 1 reply
  • August 26, 2021

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());

Andy_Cloke
Forum|alt.badge.img+17
  • Known Participant
  • 91 replies
  • August 27, 2021

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.


kuovonne
Forum|alt.badge.img+29
  • Brainy
  • 6009 replies
  • August 27, 2021

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.