# Javascript column to call API

**URL:** https://community.glideapps.com/t/javascript-column-to-call-api/57209
**Category:** Ask for Help
**Created:** [January 28, 2023, 10:58am UTC](https://community.glideapps.com/t/javascript-column-to-call-api/57209 "2023-01-28T10:58:12Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Kaiser2](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/kaiser2/32/53506_2.png) [@Kaiser2](https://community.glideapps.com/u/Kaiser2)
#### Post date: [January 28, 2023, 10:58am UTC](https://community.glideapps.com/t/javascript-column-to-call-api/57209/1 "2023-01-28T10:58:12Z")

</div>

Hi,  
I’ve tried the JavaScript column method, to run API call but is not giving any results, I’m not sure that I understand haw it is working, here is my code (API\_KEY is meant to be hided):

fetch(‘[https://api.openai.com/v1/images/generations’](https://api.openai.com/v1/images/generations%E2%80%99), {  
method: ‘POST’,  
headers: {  
‘Content-Type’: ‘application/json’,  
‘Authorization’: ‘Bearer API\_KEY’  
},  
body: JSON.stringify({  
prompt: “p1”,  
model: “image-alpha-001”,  
size: “256x256”  
})  
})  
.then(response =\> response.json())  
.then(data =\> {  
// Extract the image URL from the JSON object  
let imageUrl = data.data[0].url;

// Use the image URL to display the image in app  
document.getElementById(“myImage”).src = imageUrl;  
});

---

<div class="post-metadata">

### Author: ![Kaiser2](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/kaiser2/32/53506_2.png) [@Kaiser2](https://community.glideapps.com/u/Kaiser2)
#### Post date: [January 28, 2023, 11:03am UTC](https://community.glideapps.com/t/javascript-column-to-call-api/57209/3 "2023-01-28T11:03:04Z")

</div>

Webhooks seems to be only for trigerring and not to for post method that I need

---

<div class="post-metadata">

### Author: ![gvalero](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/gvalero/32/1037_2.png) [@gvalero](https://community.glideapps.com/u/gvalero)
#### Post date: [January 28, 2023, 12:47pm UTC](https://community.glideapps.com/t/javascript-column-to-call-api/57209/4 "2023-01-28T12:47:35Z")

</div>

Hola @Kaiser2

You were close to get a good result but you need to change some things.

Take a look at this syntax and let me know what you get

> [@Fetch json with token](https://community.glideapps.com/t/fetch-json-with-token/53097/7):
>
> Hola! I think it’s possible but if you share your cURL command (or Fetch syntax) we can try to fix it. Your JS code might look like: var data= await fetch('YOUR\_URL', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': 'YOUR\_BearerToken' }, }); const json = await data.text(); // or data.json(); return json Saludos!

Saludos!

---

<div class="post-metadata">

### Author: ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)
#### Post date: [January 28, 2023, 1:22pm UTC](https://community.glideapps.com/t/javascript-column-to-call-api/57209/5 "2023-01-28T13:22:04Z")

</div>

One thing to be aware of with this approach is that while you can probably get it to work with a JavaScript column, it won’t be secure. This is because the API call is executed client side, which means that any user of your App could potentially discover your API key - if they know how.

I believe that later this year Glide will give us some more options for doing this sort of thing more securely. In fact I wouldn’t be surprised if we get a direct integration option with OpenAI.

In the meantime, I think if I was to try something like this I’d probably delegate the API call to a 3rd party. Something like Make. So a flow like:

- Webhook from Glide to Make
- API call from Make to OpenAI
- Parse the response in Make and post the data back to Glide via the Glide API

There might be a slight delay using this approach, but I wouldn’t imagine it would be any more than a few seconds.

---

<div class="post-metadata">

### Author: ![gvalero](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/gvalero/32/1037_2.png) [@gvalero](https://community.glideapps.com/u/gvalero)
#### Post date: [January 28, 2023, 1:59pm UTC](https://community.glideapps.com/t/javascript-column-to-call-api/57209/6 "2023-01-28T13:59:42Z")

</div>

I agree!

I think we discussed this issue time ago and a good workaround could be to use a variable/parameter to hold the API key and have a better protection.

The problem is… I don’t remember where that topic is in forum 🥴🤣

---

<div class="post-metadata">

### Author: ![Abhishek\_Sinha](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/abhishek_sinha/32/52361_2.png) [@Abhishek\_Sinha](https://community.glideapps.com/u/Abhishek_Sinha)
#### Post date: [August 3, 2023, 8:13pm UTC](https://community.glideapps.com/t/javascript-column-to-call-api/57209/7 "2023-08-03T20:13:49Z")

</div>

I have a similar requirement but have no clue on js. Both the following give no return.  
Both URLs and Headers work fine through [Make.com](http://Make.com). @gvalero

```auto
fetch('https://dir.aviapages.com/api/countries/?ordering=name', {
  method: 'GET',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'xxxx'
  }
})
  .then(response => response.json())
  .then(data => {
    // Handle the response data here
    console.log(data);
  })
  .catch(error => {
    // Handle any errors here
    console.error(error);
  });

```

```auto
var data= await fetch('https://dir.aviapages.com/api/countries/?ordering=name', {
    method: 'GET',
    headers: {
        'Content-Type': 'application/json',
        'Authorization': 'xxxx'
    },
});

const json = await data.text(); // or data.json();   
return json

```

Any help ?

---

<div class="post-metadata">

### Author: ![gvalero](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/gvalero/32/1037_2.png) [@gvalero](https://community.glideapps.com/u/gvalero)
#### Post date: [August 3, 2023, 8:22pm UTC](https://community.glideapps.com/t/javascript-column-to-call-api/57209/8 "2023-08-03T20:22:03Z")

</div>

Hola!

BTW, don’t you have a CORS issue?

If so, see this thread to help you [Google maps distance matrix - #12 by gvalero](https://community.glideapps.com/t/google-maps-distance-matrix/42198/12)

---

<div class="post-metadata">

### Author: ![david](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/david/32/62831_2.png) [@david](https://community.glideapps.com/u/david)
#### Post date: [August 11, 2023, 7:20pm UTC](https://community.glideapps.com/t/javascript-column-to-call-api/57209/9 "2023-08-11T19:20:13Z")

</div>

Here’s the solution: [🆕 Call API: call custom APIs, and return data back to your app](https://community.glideapps.com/t/call-api-call-custom-apis-and-return-data-back-to-your-app/64938)

---

<div class="post-metadata">

### Author: ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/system/32/53398_2.png) [@system](https://community.glideapps.com/u/system)
#### Post date: [August 18, 2023, 7:20pm UTC](https://community.glideapps.com/t/javascript-column-to-call-api/57209/10 "2023-08-18T19:20:13Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
