Explore APIs using the browser and the command line
To get intuition of how APIs work, we will explore two web-based APIs using the browser and the command line. We will explore Spotify and Twilio APIs using GET and POST methods.
Spotify: Search API (using GET)
-
Click on
Discover features. Check the features provided by spotify
API.
Select the
Search feature, then click on
read the docs. This
API allows you to get Spotify Catalog information about albums, artists, playlists, tracks, shows or episodes that match a keyword string.
-
If you click on the
API link above, you get an error message. This is because
some parameters are required when calling an
API.
Inspect the
Request and
Response parts to understand how the
API can be invoked and what response to expect.
-
The
OAuth parameter is required which means that this
API needs an authentication.
Generate an access token by scrolling down and clicking on
get token. Leave all boxes uncheckef and click
request token. Follow the steps to sign in or sign up.
Add keywords to your search by changing the parameter q.
You can filter the results by type. Add comma separated types (valid types are: album , artist, playlist, track, show and episode)
click on
try it
Inspect the answer returned in
json format . The json object
artists contains the array of items returned. Each item contains a collection of name/value pairs. For detailed information, check the
Response section of the
search for an item API .
Let's explore some of the results. Visit the first link of the key
spotify as shown below.
This is the spotify home page of the first returned result. There are multiple items in the returned json response which means that the search query returned multiple artists for Tania Bowra
You can invoke the
API from a command line. Copy the
curl command generated in your browser (on the right hand side). Notice that this is a GET method sent with parameters
Twilio API: Send SMS API (using POST)
Twilio is a comapny for developers that helps abstract away telephone communications. It lets you bring communications in your applications. It allows you to make and receive calls, and text messages, etc.
-
Verify your phone number and follow the registration steps as shown below.
The first thing you need to do is to buy a free trial number. click on
get a trial phone number, then
choose this number.
We will use the
messaging API to send an sms. click on
explore products and choose
messaging.
At the page bottom, read what you can do with a trial account.
Click on
start sending messages. Give a name to your first messaging service.
click on
Add senders. Select
Phone number for sender type. Check your trial phone number, then
Add phone numbers.
Go back to
Messaging. click on
Try it out , then
send an SMS.
Let's test this
API. Fill in
from messaging service SID. Write your text in
Body Text and click
send test sms.
Let's invoke this
API from the command line. Check the generated curl command on the right. Click on
Show Auth Token. If the authentication key is not added to the curl command, you have to add it manually. To do so, go to console, and click on your account. Under
AUTH TOKEN, click on
show to copy and add the key to the command.
curl "https://api.twilio.com/2010-04-01/Accounts/your_account_sid/Messages.json" -X POST --data-urlencode "To=+33XXXXXX" --data-urlencode "Body=Hello again" --data-urlencode "MessagingServiceSid=messaging_service_id" -u your_account_sid:your_auth_token
Replace
your_account_sid,
your_auth_token,
messaging_service_id and
+33xxxxxx with your actual values. To understand this curl command and how to invoke this
API, check the
API documentation at
https://www.twilio.com/docs/sms/send-messages or by clicking on
Docs and support from your dashboard. The
API indicates that you need to make an HTTP POST to your account's Message resource. You need to create a message resource
/2010-04-01/Accounts/{AccountSid}/Messages.json. You have to include the three parameters
To,
From (or
messaging service sid), and
Body, in addition to the authentication key
Back to top