1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PokeAPIとは

Posted at

PokeAPI)とは?

ポケモンのデータを提供する無料のRESTful API。

主な特徴

  • 無料で使える(ただし、過負荷をかけないように注意)
  • RESTful API(エンドポイントにリクエストを送るだけでデータを取得可能)
  • JSON形式でデータを返す
  • ポケモンの基本情報(名前・画像・ステータスなど)を取得可能
  • 進化や技、特性、タイプなどの詳細情報も取得可能

使い方

PokeAPIのエンドポイントにリクエストを送ると、JSON形式でデータを取得できる。

例:ピカチュウの情報を取得

GET https://pokeapi.co/api/v2/pokemon/pikachu

レスポンスの例(省略版)

{
  "id": 25,
  "name": "pikachu",
  "height": 4,
  "weight": 60,
  "types": [
    {
      "type": {
        "name": "electric"
      }
    }
  ],
  "sprites": {
    "front_default": "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/25.png"
  }
}

→ ピカチュウのID、高さ、重さ、タイプ(Electric)、画像URLなどが取得できる。

PokeAPIでできること

1. ポケモン情報の取得

  • https://pokeapi.co/api/v2/pokemon/{pokemon_name_or_id}
  • 例: /pokemon/25(IDで指定)や /pokemon/pikachu(名前で指定)

2. ポケモンの進化情報を取得

  • https://pokeapi.co/api/v2/evolution-chain/{id}
  • 例: /evolution-chain/10(キャタピーの進化情報)

3. ポケモンの技一覧を取得

  • https://pokeapi.co/api/v2/move/{move_id_or_name}
  • 例: /move/thunderbolt(かみなり)

4. ポケモンの特性(アビリティ)を取得

  • https://pokeapi.co/api/v2/ability/{ability_name_or_id}
  • 例: /ability/static(せいでんき)

PokeAPIの活用例

  • ポケモン図鑑アプリ
  • 対戦シミュレーター
  • ポケモンのステータス表示ツール
  • ポケモン情報をランダム表示するアプリ

PokeAPIをSwiftで使う

URLSessionを使ってPokeAPIのデータを取得できる。

Swiftでピカチュウのデータを取得

import Foundation

struct Pokemon: Decodable {
    let name: String
    let height: Int
    let weight: Int
    let types: [TypeEntry]

    struct TypeEntry: Decodable {
        let type: TypeName
    }
    
    struct TypeName: Decodable {
        let name: String
    }
}

func fetchPokemon() {
    let url = URL(string: "https://pokeapi.co/api/v2/pokemon/pikachu")!
    
    URLSession.shared.dataTask(with: url) { data, response, error in
        guard let data = data, error == nil else {
            print("エラー:", error?.localizedDescription ?? "不明なエラー")
            return
        }
        
        do {
            let pokemon = try JSONDecoder().decode(Pokemon.self, from: data)
            print("名前: \(pokemon.name)")
            print("高さ: \(pokemon.height)")
            print("重さ: \(pokemon.weight)")
            print("タイプ: \(pokemon.types.map { $0.type.name }.joined(separator: ", "))")
        } catch {
            print("デコードエラー:", error.localizedDescription)
        }
    }.resume()
}

fetchPokemon()

出力例

名前: pikachu
高さ: 4
重さ: 60
タイプ: electric

まとめ

PokeAPIは、ポケモンのデータを取得できる無料のRESTful API。基本情報から進化、技、特性まで多くのデータにアクセスできる。JSON形式でレスポンスを返し、シンプルなGETリクエストで利用可能。

主な活用例として、ポケモン図鑑アプリや対戦シミュレーターなどが考えられる。Swiftを使えば、簡単にデータを取得して活用できる。

📌ポイント

  • 無料で使える(過負荷注意)
  • REST API で簡単にデータ取得
  • ポケモンの詳細情報が得られる
  • アプリやツールの開発に活用可能

PokeAPIを活用すれば、ポケモン関連のアプリやサービスを手軽に作れる。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?