Calling REST API
In this lesson, we cover calling REST API in detail.
In this section, you’ll learn how to access TMDB API to fetch popular movies.
What is an API?
API is an acronym for Application Programming Interface. An API lets two applications talk to each other.
The Movie DB contains information about popular movies and TV shows. API is the way to facilitate the app to fetch information from this big movie data storage into our application.
API key
An API key is a unique identifier given to a user, app, or developer to be able to make API requests. It is used to track the number of requests made by the app.
In the code, we’ll be using a variable apiKey
to store the API key to access TheMovieDB API.
final apiKey = "YOUR_API_KEY";
First, you’ll need to get an API key for yourself. You need this to be able to make API requests to TMDB servers. Once you have your own API key, don’t forget to replace it with “YOUR_API_KEY”.
Getting an API key
Follow these steps to request an API key from TMDB API:
- Create a free account here.
- Check your email to verify your account.
- Visit the API Settings page in your Account Settings and request an API key. You should now have an API key and be ready to go!
- Update API key in code.
Accessing API
Now that you have your API key, keep it safe & handy to use in your code later for “YOUR_API_KEY”.
Configuration
Flutter uses the http
Flutter package to make HTTP requests.
Adding package in pubspec.yaml
Add this package under the dependencies
section. Be careful about indenting yaml
file:
dependencies:
flutter:
sdk: flutter
http: ^0.12.0+1
Importing the package
We’ll be making HTTP requests from code to fetch remote movie data. To do so, we need the Dart’s http
package available to the code.
Import the http
package in your code. Use the library prefix as
for readability.
import 'package:http/http.dart' as http;
API endpoint
An endpoint is a place where the resource lives and where an API sends requests. Usually, an endpoint is a URL of the service.
The API endpoint or URL to request the most popular movies is /discover/movie?sort_by=popularity.desc
Replace
YOUR_API_KEY
with your own key generated in last lesson.
final apiKey = "YOUR_API_KEY";
final apiEndPoint =
"http://api.themoviedb.org/3/discover/movie?api_key=${apiKey}&sort_by=popularity.desc";
Your endpoint is ready to make an HTTP request.
Make HTTP Request
The package http
imported earlier is used to make the HTTP request in the code snippet below.
The await
keyword is used to make network calls asynchronously without blocking the main thread.
The following code returns the API response of HttpResponse
type. Printing it will print a text string ‘Instance of Response.’
import 'package:http/http.dart' as http;
final apiResponse = await http.get(apiEndPoint);
Run code
At this point, we’ll print apiResponse
fetched from API. It’ll simply print ‘Instance of Response’ on the screen.
Look out for the //NOTE comment(s).
Don’t forget to replace
YOUR_API_KEY
with your own API key to make requests to the TMDB API.
Please note that Android Emulator might take longer (~3 minutes) to start.
Get hands-on with 1400+ tech skills courses.