How to use an API

Last Updated Jul 13, 2021
Emma Jagger

Engineer, maker, Google alumna, CMU grad

An API, or Application Programming Interface, is a set of rules that define how computers and applications can communicate with each other. If you recall the old switchboards that operators used to field phone calls to their destinations, APIs are a little like that. A client makes a request to an API at an endpoint which it has access to. The API validates the request and passes the request to the destination server or program. The server sends a response back, first through the API, which then transfers it back to the client.  

How can we get started sending our own client requests? What are the first steps in actually using an API?  

How to use an API

To use an API, we issue HTTP requests using methods defined in the REST API architecture. This allows clients and servers to speak the same language.

Try an HTTP GET request

A GET request reads information from a record in the database. Let's try this with the Holidays API. Typing `curl 'https://holidays.abstractapi.com/v1/?api_key={YOUR_API_KEY}&country=US&year=2020&month=12&day=25` and ENTER in your terminal should return a status code and content that looks like this:


[
    {
        "name": "Christmas Day",
        "name_local": "",
        "language": "",
        "description": "",
        "country": "US",
        "location": "United States",
        "type": "National",
        "date": "12/25/2020",
        "date_year": "2020",
        "date_month": "12",
        "date_day": "25",
        "week_day": "Friday"
    }
]

Note on `{YOUR_API_KEY}`: You will need a free API key from Abstract API to accomplish the above request.

So, what happened in this GET request? The client (us) requested information from the API at `https://holidays.abstractapi.com/v1/` by sending a GET request to the API. The additional request parameters `&country=US&year=2020&month=12&day=25` asked for public holidays in the US (`country=US`), in the year 2020 (`year=2020`), the month of December (`month=12`), and the 25th day (`day=25`). The API authenticated the request and made a call to the server. The server returned the requested information to the API, and the API sent the JSON card with the requested information back to the client (us).

API authentication

JSONPlaceholder is a helpful test resource, but what about API authentication? Most APIs require you to sign up for API keys to use their services. They will get a public key and a private key. The public key is to be used in request headers and URLs (where they will be visible), and a private key, to be used in server-to-server communication.  

Getting an API key is similar to signing up for any other service. We will use the Abstract API as an example. Go to the Abstract API and select Create Your Free Account. Enter your information and select Sign Up. You will receive a confirmation email with a link to your Abstract API dashboard. Navigate to any of the APIs listed, for example, the IP Geolocation API. This page displays your unique API key for this API, and has a host of console options for testing it out.

Testing your new token

You can test this API key in two ways:

  • A quick cURL request in your terminal: `https://holidays.abstractapi.com/v1/?api_key={YOUR_API_KEY}`
  • Use the in-browser Abstract API console to test your new API with your new credentials.  

Conclusion

When you're just starting your web development journey, the API authentication can seem like a pain, but it's usually just a quick email application and you're in. Now your next step is learning the other requests you can perform in REST (like POST, PUT, and DELETE), and how to use APIs in your code to build efficient, powerful software.

Related articles

No items found.
Try Abstract's multiple free API's for free.
Get started