Dedicated to the Hydrogen framework, headless commerce, and building custom storefronts using the Storefront API.
We were using the Shopify Buy SDK for Unity, but it only works up to 2019, and we have heard for sure from someone at Shopify they will no longer support or update it. So, I'm trying to figure out how to get things working using the Storefront API.
This is not my usual type of programming, and I'm stumbling just getting a basic http request working. I get a 400 Bad Request error, which is not super helpful.
Can someone look at this snippet and see if anything is obviously wrong? If I can get past this point, I think I'll be fine...
private string storefrontAccessToken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
private string storeURL = "https://shopname.myshopify.com/api/2023-01/graphql.json";
//I have the correct token and store url in the actual code, and that is the shopify version we are on...
// Make a GraphQL POST request
private IEnumerator MakeRequest(string query, System.Action<string> callback) {
using (UnityWebRequest request = new UnityWebRequest(storeURL, "POST"))
{
request.SetRequestHeader("Content-Type", "application/json");
request.SetRequestHeader("X-Shopify-Storefront-Access-Token", storefrontAccessToken);
var queryJSON = new {
query = query
};
string jsonBody = JsonUtility.ToJson(queryJSON);
byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonBody);
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
request.downloadHandler = new DownloadHandlerBuffer();
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.Success)
{
callback(request.downloadHandler.text);
}
else
{
Debug.Log("Error: " + request.error);
}
}
}
public void BasicTest(System.Action<JSONNode> onSuccess) {
string query = "{ shop { name } }";
StartCoroutine(MakeRequest(query, (response) => {
JSONNode jsonResponse = JSON.Parse(response);
string shopName = jsonResponse["data"]["shop"]["name"];
onSuccess(shopName);
}));
}
did you get solution?
I'm trying to do the same thing in Unity and having trouble too. I got a 400 response due to the query not being valid or not being valid for the Admin API and NOT the Storefront API, so maybe check that the query is valid. I'm having issues parsing the JSON response in C# in order to get the data. I wish there were more tutorials and docs for this in Unity.