How to get started with APIs?

So I want to learn how to be able to get data from websites and how to perform actions on websites through a program (Java or Python preferably) like getting prices of things, making an order, sending messages/emails, etc. I know that an API can be used to get data from websites but API is more of a buzzword to me, I don’t know anything about how to use them or even a solid grasp of what they really are. I’ve tried looking this stuff up but it seems every article just overloads you with information that I don’t know how to process. I don’t have any prior knowledge or experience when it comes to this stuff. Anyone have any good places to start learning how to program this kind of stuff? Not only APIs, but anything that has to do with program-website interaction would be really appreciated.

Thank you in advanced for anything you’re able to provide

2 Likes

The key word you are looking for is REST. Some tutorials:

1 Like

You use an API by making an HTTP request to the API’s web address. Depending on the API endpoint, the type of HTTP request you make, and the data you send with the HTTP request will determine what you’re doing with the API. Instead of waiting for someone here to write a detailed explanation of how to use an HTTP request to call an API, you’re probably better off googling “how to make an HTTP request in [language]” and going from there. If you have more specific questions then you can come back here and it will be easier for people to answer.

I’d recommend looking at the Blue Alliance API. That’ll help you get past some of the information overload, as you’re already familiar with everything that’s being accessed. You can focus in on some specific piece of data and ignore everything else.
Overview:

Details:

Conceptually, it’s pretty easy - you call a URL (There are a LOT of possible URL’s in the TBA API - they all start with https://www.thebluealliance.com/api/v3/, and have some variables to plug in, indicated by curly brackets), and it returns some information. In reality, it can be a little more difficult :). The following is the basics of doing it in Java.

This function would be used to get information from an appropriate URL (for example, to get the basic team info for my team from TBA, it would be https://www.thebluealliance.com/api/v3/team/frc2177/simple). All you need to run it is the URL and your x-auth key from your TBA accounts page.

public InputStream getjSON(String path)
{
    System.out.println("Fetching from URL: " + path);
    InputStream stream = null;
    try
    {
    	URL url = new URL(path);
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
		connection.setRequestMethod("GET");
		connection.setRequestProperty("User-Agent", "TBA-API");
		connection.setRequestProperty("X-TBA-Auth-Key", auth); //x-auth key from TBA accounts page
		connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		connection.setRequestProperty("charset", "utf-8");
		connection.setUseCaches(false);
    	
        stream = (InputStream)connection.getInputStream();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    return stream;
}

It uses all native Java stuff, and returns an InputStream that you can iterate through to read the results. That’s basically it - with that simple function, we can access the API using the appropriate URL and get back any information we want. Calling that for https://www.thebluealliance.com/api/v3/team/frc2177/simple returns the following:

{
  "city": "Mendota Heights",
  "country": "USA",
  "key": "frc2177",
  "name": "Boston Scientific/PTC/Palmer Family&Visitation School",
  "nickname": "The Robettes",
  "state_prov": "Minnesota",
  "team_number": 2177
}

Parsing through that can be a pain. I prefer to use a package called Jackson to help with the parsing. It lets me create a simple POJO for the information:

public class TeamSimple 
{
    public String key;
    public int team_number;
    public String nickname;
    public String name;
    public String city;
    public String state_prov;
    public String country;
}

Then It’s a simple call using the Jackson class com.fasterxml.jackson.databind.ObjectMapper class to parse the InputStream into the POJO:

TeamSimple result = mapper.readValue(getjSON("https://www.thebluealliance.com/api/v3/team/frc2177/simple"), TeamSimple.class);

After that, you can do whatever you want, because you now have the data in an easy to access java object!

4 Likes

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.