Using Unity for subscription lists

Has anyone managed to do that besides mailchimp? i have found a script that works for it : GitHub - fiftytwo/MailChimpSubscriber: Subscribe users to your MailChimp list right from your Unity game.

but im trying to use klaviyo and i just cannot make it work. i have read through their api for post requests but had no luck in either using the above script or using the one i have for the leaderboard, which is this ( dont work)

private IEnumerator SendData()
    {

        WWWForm form = new WWWForm();
        form.AddField("email", emailField.text);
        form.AddField("api_key", "my api key goes here");
        UnityWebRequest www = UnityWebRequest.Post("https://a.klaviyo.com/api/v2/lists?api_key=my api key", form);
// i have also tried it like this : 
//UnityWebRequest.Post("https://a.klaviyo.com/api/v2/list/{LIST_ID}/subscribe

        yield return www.SendWebRequest();

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

i either get a 404 or a 400 error code. now if i use the mailchimp script i get an error with the api key format

As with all network code, get it working with Postman or curl first. That makes it NOT a Unity problem. Solve the networking first. You must solve that or anything else is irrelevant.

Once you have that working, then try and port it to UnityWebRequest. If it still doesn’t work, one handy debug solution is to hook up a proxy like Charles and compare the functioning output from Postman / curl to the output from Unity, and from that reason about where your issues are.

1 Like

thanks for the reply, i managed to post a request through postman and now am looking for a way to put a json string into unity so i can link it and try it out! Basiclay on postman this is the url that posts the request

https://a.klaviyo.com/api/v2/list/YeXzKp/members?api_key=pk_ec448e1bdab7504c143466ca5eeabc5e95&profiles=[]

but it also needs a json text

 {
"profiles" :[
        {
            "email": "george.washington@example.com"
        }

    ]
}

which im now trying to find out how to make the value be the unity string data i need

I’m guessing you MIME encode the JSON and add it as a field to the form?

well im looking into it! found some threads here doing that but yeah i still need to fully understand it and try it out! unless you have a general idea and would be willing to help once more!!

ive gotten this far

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class MailingScript : MonoBehaviour
{
    public InputField email;

    [Serializable]
    public class Email
    {
        public string email;
    }
    public IEnumerator Login(string bodyJsonString)
    {
        Email playerInstance = new Email();
        playerInstance.email = email.text;
        string playerToJson = JsonUtility.ToJson(playerInstance);
        Debug.Log(playerToJson);
        Debug.Log("String"+ bodyJsonString);
        email.text = bodyJsonString;
        UnityWebRequest req = UnityWebRequest.Post("https://a.klaviyo.com/api/v2/list/YeXzKp/members?api_key=pk_ec448e1bdab7504c143466ca5eeabc5e95&profiles=[]", bodyJsonString);
        req.SetRequestHeader("content-type", "application/json");
        yield return req.SendWebRequest();
        if (req.isNetworkError || req.isHttpError)
        {
            Debug.Log(req.error);
        }
        else
        {
            Debug.Log("Form upload complete!");
        }

    }

    public void Upload()
    {
        StartCoroutine(Login(email.text));
    }


}

which debugs the text email but i get an error 400 bad request. and i think its due to the json not being as i initialy posted?

im going to try once more in case someone can help! i got to a point i can make my string to json so i tried posting it but i still get the 400 error!

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class MailingScript : MonoBehaviour
{
    public InputField emailText;
    [Serializable]
    public class Profile
    {
        public string email;
    }
    [Serializable]
    public class MyClass
    {
        public Profile[] profiles;
    }
   
    IEnumerator SaveData()
    {
        MyClass myClass = new MyClass();
        myClass.profiles = new Profile[1];
        myClass.profiles[0] = new Profile();
        myClass.profiles[0].email = emailText.text;
        string json = JsonUtility.ToJson(myClass.profiles[0]);
        UnityWebRequest www = UnityWebRequest.Post("https://a.klaviyo.com/api/v2/list/YeXzKp/members?api_key=pk_ec448e1bdab7504c143466ca5eeabc5e95&profiles=[]", json);
        www.SetRequestHeader("content-type", "application/json");
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
        }
        else
        {
            Debug.Log("Form upload complete!");
        }
        Debug.Log(json);
    }
    public void Subscribe()
    {
        StartCoroutine(SaveData());
    }
}