How to create a rest authentication header in unity

Hey,

So I am trying to link https://avwx.rest/ to my project. I have implemented the API fine and it works as expected, the only issue I have is that I have no idea where to place the authentication script. Here is my code:

public class METARScript : MonoBehaviour
{
 
 
    private readonly string AVWXURL = "https://private-anon-f687e87c21-avwx.apiary-mock.com/api/metar/egss";
 
    private void Start()
    {
        StartCoroutine(loadMETAR());
    }
 
    IEnumerator loadMETAR()
    {
        UnityWebRequest METARInfoRequest = UnityWebRequest.Get(AVWXURL);
 
        yield return METARInfoRequest.SendWebRequest();
 
        if(METARInfoRequest.isNetworkError || METARInfoRequest.isHttpError)
        {
            Debug.Log(METARInfoRequest.error);
            yield break;
        }
 
        JSONNode METARInfo = JSON.Parse(METARInfoRequest.downloadHandler.text);
 
        string METAR = METARInfo["sanitized"];
 
        print("The METAR for EGSS is: " + METAR);
    }
}

I am trying to add the authentication key: --stripped-- which will give permission to access the data on the server.

Can anyone shed any light on how to do this?

Thanks

Well, just have a look at the documentation of that service. There are generally two ways you can provide your API key / token. First is inside an Authorization header the second is through a get url parameter. If you want to use an header, just follow the instructions. You can use any prefix or just the key itself. So they are actually quite flexible on the server side.

So this is the first part, to know how the API expects the key. Second is look up how you can set a request header when using a UnityWebRequest. So the first point would be the documentation. You will notice that the UnityWebRequest has a method called SetRequestHeader. The usage should be straight forward. So just do

METARInfoRequest.SetRequestHeader("Authorization", yourAPIKey);

or like in the example on the API documentation page you can also add a prefix like:

METARInfoRequest.SetRequestHeader("Authorization", "TOKEN " + yourAPIKey);

Of course this line has to be executed before you call “SendWebRequest()”.

Alternatively you can just pass it as a url parameter

UnityWebRequest METARInfoRequest = UnityWebRequest.Get(AVWXURL + "?token="+yourAPIKey);