Answered

Trying to use Fetch() with Javascript

  • 25 October 2021
  • 5 replies
  • 765 views

Hello, I’m trying to access the API using fetch in javascript but keep getting “TypeError: Failed to fetch”, I’m having a hard time figuring out how to do this with the documentation. Can anyone provide me any resources or info on this?

 

fetch("https://api.typeform.com/forms/{form_id}/responses", {
method: 'GET',
headers: {
'X-API-KEY': '{My secret key here}'
}})
.then(res => res.json())
.then(data => console.log(data))
.catch(error => console.error(error));

 

icon

Best answer by Liz 25 October 2021, 20:08

View original

5 replies

Userlevel 7
Badge +5

Hi @Chiken Happy Monday! Is the API token you’re using the one that matches the account the Typeform is located under? For example, if you have a shared workspace, you’ll need to use the API key from the owner of the shared workspace. 

@picsoung might also have some insight into this error, too. 

@Liz Thanks and you as well!
I am using a token that was created by the owner of the workspace!

Also some information I should have added is that I am currently running a development server off http://localhost:3000, so I’m not sure if the server is automatically denying me because of that? 

Access to fetch at 'https://api.typeform.com/forms/{form_id}/responses' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Userlevel 7
Badge +5

Hi @Chiken Ah thank you! Yes, that’s definitely causing the issue. Our APIs don’t allow for sending calls on the front-end or through the manner you are. You’ll need to be sending them directly through the backend. 

Hi @Chiken Ah thank you! Yes, that’s definitely causing the issue. Our APIs don’t allow for sending calls on the front-end or through the manner you are. You’ll need to be sending them directly through the backend. 

 @Liz This was a major help, thank you so much. Now I can offload the work to my backend team :grin:
Haha, thanks and have a great week!

Userlevel 7
Badge +5

HAHAHAH I’m happy to be your excuse for delegating work to the backend team ha! Let me know if I can help answer anything else, @Chiken . :joy::joy:

Reply