In this section, we will be exploring various APIs that are available on the web, send requests to them, and receive responses, before explaining how they work via the Python programming language.
Let's consider the following sample URL, https://www.someexampledomain.com. The API it provides comes with parameters, locators, and authentication. By using these, we can access the following resources:
- https://api.someexampledomain.com
- https://api.someexampledomain.com/resource?key1=value1&key2=value2
- https://api.someexampledomain.com/resource?api_key=ACCESS_KEY&key1=value1&key2=value2
- https://api.someexampledomain.com/resource/v1/2019/01
Parameters or collections of key-value pairs are actually sets of predefined variables that are provided by the web. Usually, the API provides some sort of documentation or basic guidelines regarding its usage, HTTP methods, available keys and types, or permitted values that the key can receive, along with other information on the features that are supported by the API, as shown in the following screenshot:
End users and systems can only use the API with the features and functions that the provider permits.
The following a number of actual API links and example calls that show the formats and parameters that are used in URLs:
- http://api.walmartlabs.com/v1/reviews/33093101?apiKey={apiKey}&lsPublisherId={Your LinkShare Publisher Id}&format=json
- https://api.nasa.gov/neo/rest/v1/feed?start_date=START_DATE&end_date=END_DATE&api_key=API_KEY
- https://api.sunrise-sunset.org/json?lat=36.7201600&lng=-4.4203400&date=today
- https://api.twitter.com/1.1/search/tweets.json?q=nasa&result_type=popular
- http://api.geonames.org/postalCodeSearchJSON?postalcode=9011&maxRows=10&username=demo
- http://api.geonames.org/postalCodeSearch?postalcode=9011&maxRows=10&username=demo
- https://api.nytimes.com/svc/mostpopular/v2/viewed/1.json?api-key=yourkey
- https://maps.googleapis.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&zoom=13&size=600x300&maptype=roadmap markers=color:blue%7Clabel:S%7C40.702147,-74.015794&markers=color:green%7Clabel:G%7C40.711614,-74.012318&markers=color:red%7Clabel:C%7C40.718217,-73.998284&key=YOUR_API_KEY
Parameters such as key, api_key, apiKey and api-key are required for security and tracking measures and need to be obtained before you process any API requests.