Skip to main content
Solved

Help with connecting an api to Unity Game Engine


Forum|alt.badge.img+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!

Best answer by caroline

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

View original
Did this topic or the replies in the thread help you find an answer to your question?

6 replies

caroline
Forum|alt.badge.img+8
  • Klaviyo Alum
  • 215 replies
  • March 8, 2021

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


Forum|alt.badge.img+2
  • Author
  • New Member
  • 3 replies
  • March 8, 2021

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

 


caroline
Forum|alt.badge.img+8
  • Klaviyo Alum
  • 215 replies
  • Answer
  • March 9, 2021

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


Forum|alt.badge.img+2
  • Author
  • New Member
  • 3 replies
  • March 9, 2021

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

caroline
Forum|alt.badge.img+8
  • Klaviyo Alum
  • 215 replies
  • March 10, 2021

Hi @slaga,

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

Thanks,

Caroline


caroline
Forum|alt.badge.img+8
  • Klaviyo Alum
  • 215 replies
  • March 17, 2021

Hi @slaga,

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

Best,

Caroline