Posts


Pokemon API haunter



This is one of the coolest APIs to explore if you are a fan of Pokemon or were a fan growing up.
We will be using the RESTful Pokémon API.

it is very easy to use and to understand. We will be creating a small app using this API.

Change and pick your favorite Pokemon, click on "Submit my changes" and get to see your favorite Pokemon's infos, plus a nice image of your favorite Pokemon from the Pokemon database !



pokeball Pokemons pokeball


pokemon

Abilities of the Pokemon : static; lightning-rod;

Type of the Pokemon : electric

Height of the Pokemon : 4

Weight of the Pokemon : 60

Moves of the Pokemon : take-down; sweet-kiss; swagger; dynamic-punch; pay-day; round; slam; encore; focus-punch; double-kick; natural-gift; nasty-plot; endure; thunderbolt; brick-break; thief; echoed-voice; bide; surf; substitute; body-slam; captivate; mega-kick; double-edge; play-rough; return; submission; covet; flash; skull-bash; snore; double-team; thunder-punch; rage; draining-kiss; reversal; zap-cannon; discharge; curse; secret-power; laser-focus; nuzzle; volt-tackle; spark; wild-charge; mud-slap; rest; thunder-shock; detect; grass-knot; toxic; signal-beam; fling; play-nice; swift; volt-switch; counter; shock-wave; light-screen; frustration; iron-tail; calm-mind; agility; seismic-toss; rollout; rising-voltage; mega-punch; electroweb; uproar; thunder-wave; magnet-rise; growl; knock-off; rock-smash; charm; dig; electric-terrain; hidden-power; helping-hand; strength; confide; tail-whip; mimic; electro-ball; rain-dance; quick-attack; attract; feint; facade; charge-beam; protect; headbutt; reflect; sleep-talk; thunder; defense-curl;




How does it work ?

(Skip this if too technical)

I am using the Requests library in Python to access the Pokemon API.
This is the url that I am accessing. You can see that the url renders some data in a JSON format.

We create a list in Python to get all the Pokemon names from this data. We send this information to the HTML page and into the HTML form - I created a Bootstrap Dropdown with a submit button, to choose Pokemon names from.

Once we choose a pokemon name and we click on "Submit my changes". The page reloads, and the information about the pokemon changes.

When this happens, when clicking on the submit button, the pokemon name that we have picked gets saved (LocalStorage in Javascript) and gets sent to our Python script (Post request).

Say, we chose the pokemon "charmander".
We create a new url using the previous url and we add to it the name of the pokemon that we picked, in this case "charmander". This is the new url that we are accessing and as you can see there is a lot of data in it.

We are interested in getting the following Pokemon's informations : Abilities, Moves, Height, Weight, Type (type of the pokemon) and Sprite (image of the pokemon, I choose to get the "front-defaut" image).

We create lists and variables to store these Pokemon informations and then we send these to our HTML file once again.
Flask Python with its Jinja2 template engine takes care of this for us so that whenever we pick a different pokemon, the information of that pokemon with its image gets displayed !


Python Code

I have used not only Python, but HTML forms and Javascript as well to make it interactive. To know more feel free to email me 📧.
This is a portion of the Python code that I have used.


import requests

url = "https://pokeapi.co/api/v2/pokemon"

data = requests.get(url)
json_data = data.json()

pokemon_names = []

for pokemon in json_data.get("results"):
    pokemon_names.append(pokemon['name'])

post_data = request.values

post_option = post_data.get("option")

abilities = []
moves = []

height = 0
weight = 0

type = ""
sprite = ""

if post_option:

json_data_url_with_pokemon = requests.get(url + "/" + post_option).json()

for value in json_data_url_with_pokemon:

    height = json_data_url_with_pokemon["height"]
    weight = json_data_url_with_pokemon["weight"]
    type = json_data_url_with_pokemon["types"][0]["type"]["name"]
    sprite = json_data_url_with_pokemon["sprites"]["front_default"]

    for elements_abilities in json_data_url_with_pokemon["abilities"]:
        abilities.append(elements_abilities['ability']['name'])

    for elements_moves in json_data_url_with_pokemon["moves"]:
        moves.append(elements_moves['move']['name'])
    

Play and explore this API, use the Requests library and explore the data !


We have successfully accessed data through an API.
I created a small app to illustrate what we do with such data !
To access other public API, you can use the same method with the Request Python library.

And with that you are able to access public APIs !
Keep in my mind that other APIs might require authentication, in which case you will need an API key and will have to add the API key in the request header!

If you have any questions or wants to know more about the project, feel free to email me or tweet at me!


Related : Data Analysis - Master's Project


Join my newsletter for similar articles and early access