Solved

Help with connecting an api to Unity Game Engine

  • 7 March 2021
  • 6 replies
  • 171 views

Badge +2

Hello everyone! im a game developer and i am making a game for a client that uses klaviyo for subscriptions. now i have handled api requests through unity before but i cannot link it to my account here to see if it works.
Here is my code  through Unity that calls a Post request
 

private IEnumerator SendData()
{
WWWForm form = new WWWForm();
form.AddField("email", emailField.text); // this is the email entered in the UI of the game
form.AddField("api_key", "my_api_key_here");
UnityWebRequest www = UnityWebRequest.Post("https://a.klaviyo.com/api/v2/list/{LIST_ID}/subscribe", form);
yield return www.SendWebRequest();

if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
Debug.Log("Registered");
}
Debug.Log(emailField.text);
}
}

 like this i get a 404 and i dont know if im doing something wrong with the url. to be honest i have tried it with mailchimp since this is what i have used before and it works ok. any help would be appreciated thanks!

icon

Best answer by caroline 9 March 2021, 19:31

View original

6 replies

Userlevel 5
Badge +8

Hi @slaga,

Thanks for sharing with the Community! Are you defining LIST_ID anywhere? Maybe you are in your actual code but redacted it for this example? Not including the list ID could result in a 404.

Best,

Caroline

Badge +2

thank you for the answer i managed to fix everything but now when i upload it on my server i get blocked by cors.

 

Userlevel 5
Badge +8

Hi @slaga,

The CORS error is likely a result of hitting Klaviyo’s server-side Lists V2 endpoint via front-end code. Instead, I would suggest using the front-end endpoint described here.

Best,

Caroline

Badge +2

indeed , i did try your frontend api 

https://manage.kmail-lists.com/subscriptions/subscribe through a form in unity acting like it fills the actual form and doing it like this with only the email parameter being taken from user input
https://manage.kmail-lists.com/subscriptions/subscribe?a=W9pKM7&g=YeXzKp but still the same Cors error, the funny thing is from my computer ( in the game engine ) everything works fine, as soon as i upload it to my server or run it on localhost it gets blocked.you can try it your self in https://slaga-games.com/shoot
Userlevel 5
Badge +8

Hi @slaga,

Can you post the CORS error? I’m not seeing it when I test.

Thanks,

Caroline

Userlevel 5
Badge +8

Hi @slaga,

Just checking in here -- were you able to resolve the CORS error? If not, can you post the error here?

Best,

Caroline

Reply