API Principles
API Principles
info
Limitations
We will not explain simple programming concepts, it is assumed that
the user is familiar with these concepts.
UI versus API
What is a UI?
1. Menu driven
Click on the Help menu at the top of this window.
Select “Shortcuts”, this is a list of shortcuts you can use when you
edit files later in this course.
Close the shortcuts window by clicking on the x in the right corner
of the tab.
What is an API?
An Application Programmer’s Interface (API) provides a way to
communicate programmatically with a product or service. Through the
API a program can access a different program’s data or request a service.
The program making the request through an API does not need to know the
details of how the request will be handled, just how to make the request.
This is similar to driving a car, driving does not require you to understand
how the engine or the braking systems work, you just need to know which
pedal to use.
API examples
Application APIs, for example Excel, can provide access to the data the
application manages. For example, Excel provides a JavaScript API that
can be used to access worksheets, ranges, tables, and more.
Device APIs allows you to interact with the hardware your browser is
running on. For example the Geolocation API.
Software libraries provide an API to access the utilities provided by the
library. For example Python has an extensive library.
Databases provide an API for data requests. For example, the ODBC
interface provides a common way to access data in different types of
databases.
Web APIs are used to communicate between computers that are
connected over the Internet. There are client-side APIs (for example
fetching specific data from a server) and server-side APIs (returning
customized search results).
Web APIs
As we mentioned on the previous page, an API is a way for one program to
communicate with another. A web API provides a protocol for how a client
may make a request for a service or for data from a server using HTTP. A
protocol is required so that the requester knows how to formulate the
request and the receiver knows how to interpret the request.
If a website does not provide an API it makes it more difficult to access the
information on the website. Another way to access information on a
website is via a method called “web-scraping”. Pages are downloaded and
processed locally to extract information. If you see an email address with
the @ replaced by the word at this is often done to prevent the email
address from being “scraped” and added to an undesired mailing list. This
helps foil algorithms that search for an @ sign to find email addresses.
Many social media websites offer APIs. One example is YouTube, you
can use their API to upload videos to the YouTube website.
A credit card company would provide an API so that businesses can
access customer info when the customer does not have their card.
A review site such as Rotten Tomatoes provides an API so that other
websites, for example movie theaters, can post review information.
JSON-RPC uses JSON for its data format. Similar to REST, it is stateless and
similar to SOAP it is transport agnostic.
GraphQL
.guides/img/clientserver
important
What is HTTP?
Hypertext Transfer Protocol (HTTP) enables communication between
clients and servers. Hypertext refers to the fact that web pages have a
special format (HTML), and can contain links, which when clicked will
fetch a new web page.
The server provides an HTTP response that does one of these things:
- provides the client the requested resource
- informs the client that the requested action has been completed
- informs the client that an error occurred when executing the request
Scheme
The first element of a URI is the scheme. It is separated from the rest of the
URI by a colon. The scheme defines the format of rest of the URI. The
Internet Assigned Number Authority (IANA) maintains a list of official URI
schemes. The IANA is responsible for the parts of Internet that need to
coordinated in order for it to run smoothly for example, domain names.
Authority
The authority component begins with // then there is an optional user info
component followed by an @, a host subcomponent, followed by : and
finally an optional port component.
URI Path
The path component can include any number of path segments separated
by a slash (/). A path is always defined for a URI but it can be empty. In the
weather example below, the URI path is /cap/ma.php.
Query (optional)
A query consists of the object to be queried and the query to made on the
object separated by a ?.
In the example below we are querying the National Weather Service public
alerts.
A query to the NWS website
You can change the ma to be the initials of the state you are interested in.
You can also change the query as listed below.
Fragment (optional)
The fragment portion begins with a # and it can refer to a portion of a web
page. If the URI points to an HTML document the fragment might refer to
an element on the page and that element will appear in view in the
browser.
Here is an example from our docs. If you click on the link below you will be
taken to the specific section on the page. Paste the link in a browser tab to
try it out, the text to the right of the # refers to the section on the page.
https://docs.codio.com/students/accessing-
codio/faq.html#deleted-a-file-by-mistake
Access Web APIs
Many interesting websites can be accessed using the cURL command. Some
web pages pull information from various sources and combine it in a new
way. We will explore this in greater detail in assignment 1.3 of this module.
In the examples below we demonstrate how you can use a map website to
search for places by name. Then you can use the returned information to
find the coordinates of the location you are interested in and its street
address.
Using the coordinates you can search for interesting places nearby. You can
also get information about the weather, sunrise and sunset times, local
time and timezone using the coordinates.
Try some of the examples below in the terminal window on the left. There
are two versions of some of the commands, the first version uses the -i
parameter and outputs the HTTP response headers. In the second version
we are piping the output to the Linux utility jq for readability.
Find the places containing “central park” in their name in the US:
curl -i "https://nominatim.openstreetmap.org/search?
q=central+park&countrycodes=us&format=json"
curl "https://nominatim.openstreetmap.org/search?
q=central+park&countrycodes=us&format=json" | jq
curl -i "https://nominatim.openstreetmap.org/reverse?
lat=40.7827725&lon=-73.965355&format=jsonv2&namedetails=1"
curl "https://nominatim.openstreetmap.org/reverse?
lat=40.7827725&lon=-73.965355&format=jsonv2&namedetails=1" | jq
API of Wikipedia
curl "https://en.wikipedia.org/w/api.php?
action=query&list=geosearch&gsradius=1000&gscoord=40.7827725|-73
.965355&format=json&gssort=relevance&gslimit=20" | jq
curl "https://es.wikipedia.org/w/api.php?
action=query&list=geosearch&gsradius=1000&gscoord=40.7827725|-73
.965355&format=json&gssort=relevance&gslimit=20" | jq
challenge
curl "https://api.weather.gov/points/40.7828,-73.9653" | jq
Sunset/sunrise API
curl "https://api.sunrise-sunset.org/json?
lat=40.782864&lng=-73.965355&date=tomorrow&formatted=0" | jq
Time API
curl "https://timeapi.io/api/Time/current/coordinate?
latitude=40.7827725&longitude=-73.965355" | jq
curl "https://timeapi.io/api/Time/current/zone?
timeZone=Europe/Amsterdam" | jq
curl "https://timeapi.io/api/TimeZone/coordinate?
latitude=40.7827725&longitude=-73.965355" | jq
curl "https://timeapi.io/api/Conversion/DayOfTheWeek/2022-11-22"
| jq
challenge
curl -X 'POST' \
'https://timeapi.io/api/Conversion/ConvertTimeZone' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"fromTimeZone": "Europe/Amsterdam",
"dateTime": "2021-03-14 17:45:00",
"toTimeZone": "America/Los_Angeles",
"dstAmbiguity": ""
}' | jq
challenge