Author Topic: UEXcorp api requests  (Read 245 times)

electricscout

  • Newbie
  • *
  • Posts: 3
UEXcorp api requests
« on: March 31, 2025, 02:05:12 PM »
I'm very, very inexperienced with stuff like this so bear with me.

I'm trying to make a command that will run a c# script with the intention of using UEXCorps API to get the price of a specific commodity, however I've tried a few different things but really dont know where to start with this. Does anybody know how/if i can send an API get request with a VA c# script?

SemlerPDX

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 317
  • Upstanding Lunatic
    • My AVCS Homepage
Re: UEXcorp api requests
« Reply #1 on: April 01, 2025, 11:51:22 AM »
You can absolutely do this.  I use inline functions in C# to contact the OpenWeatherMap API in my AVCS SENSE profile dealing with sensors & weather.

You should be able to refer to the API documentation from UEXCorps (assuming they have good documentation) on how to contact their API to get the info you need.  A good API presents syntax and code examples, hopefully in more than one language (including C#).

If you're super new to C#, you can get a quick leg up with the basics presented by W3 Schools here:
https://www.w3schools.com/cs

electricscout

  • Newbie
  • *
  • Posts: 3
Re: UEXcorp api requests
« Reply #2 on: April 01, 2025, 01:45:31 PM »
Thank you! The documentation for UEX's api seems somewhat mediocre since it only has the url for the data you want to access and what it should return, nothing on how I actually go about sending a request to it in the first place.

SemlerPDX

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 317
  • Upstanding Lunatic
    • My AVCS Homepage
Re: UEXcorp api requests
« Reply #3 on: Today at 01:57:26 PM »
Thank you! The documentation for UEX's api seems somewhat mediocre since it only has the url for the data you want to access and what it should return, nothing on how I actually go about sending a request to it in the first place.

Not all API's are equal, and so it may require a bit more knowledge or effort on the part of users.  For example, on the OpenWeatherMap API, we must apply for an API key and then we must use that key in each URL request from the API in order to receive results - it is documented like this:


...now, the UEX API does have many pages dictating how to make calls to their API, and note that a Bearer Token is required yet does not provide any examples of where this belongs in the API request URL:


(and for the commodities prices, an actual call - shows no place to insert the Bearer Token - and I tried in various ways just for a test, came up empty)


The only thing I can think of is to contact them (or someone who already uses this API), or find an example of a working API application for UEX and see how they do things.  Wish I could help more, but their documentation seems to be lacking imho.


For example, when I need to get data from OpenWeatherMaps API, I assemble the URL with the request I need and including my API key as documented, and use this method in a C# inline function within a VoiceAttack command:
Code: (csharp) [Select]
using System.Net;

private void GetWeatherApiReturn(string apiURL, dynamic VA)
{
  VA.SetText("~api_return", null);
  string apiReturn = string.Empty;
 
  try
  {
    using (WebClient wc = new WebClient())
    {
      wc.Headers.Add("Accept-Language", " en-US");
      wc.Headers.Add("Accept", " text/html, application/xhtml+xml, */*");
      apiReturn = wc.DownloadString(apiURL);
    }
  }
  catch (WebException ex)
  {
    VA.WriteToLog("API ERROR!", "red");
    if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null)
    {
      var resp = (HttpWebResponse)ex.Response;
      if (resp.StatusCode == HttpStatusCode.NotFound)// handle HTTP 404 errors
      {
        VA.WriteToLog("HTTP 404 - Unable to find page at URL:", "red");
        VA.WriteToLog(apiURL, "blank");
      }
    }
  }
  finally
  {
    VA.SetText("~api_return", apiReturn);
  }
}

electricscout

  • Newbie
  • *
  • Posts: 3
Re: UEXcorp api requests
« Reply #4 on: Today at 03:26:34 PM »
Thank you so much. Thats so helpful, even if the documentation is less so. I really appreciate your own example and I'm sure it'll come in handy.