Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

APIを使ってみる

Posted at

APIとは?

「アプリケーション・プログラミング・インターフェース」の略。

別のサービスを取り込む

APIは「別のサービスの力を、簡単に自分のプログラムで使える」仕組み
自分のプログラムと使用したいサービスの窓口になってくれるのがAPI
有料のものと無料のものがあります。

自分のプログラムーーーーAPIーーーーー使用たいサービス

どんなことができる?

できること
会話・質問応答 ChatGPTに質問して答えを得る
翻訳 Google翻訳APIでテキストを翻訳
ニュース記事取得 NHKや朝日新聞APIで最新記事を一覧化
天気情報 気象庁やOpenWeatherMap APIで現在の天気を取得
地震速報 最新の地震データを自動取得してSlack通知
画像生成 DALL·EやStable Diffusion APIでイラストを作る
音声認識 Whisper APIで音声をテキスト化
感情分析 AIに文章を渡してポジティブ/ネガティブを判定
地図表示 Google Maps APIで地図をWebに埋め込む
経路検索 出発地と目的地を入力して最短ルートを取得
現在地表示 GPSの位置を地図にマーカーで出す
地名検索 緯度・経度から住所を表示する(逆ジオコーディング)
為替レート取得 「1ドル=何円?」を自動で取得
株価情報 Appleやトヨタの株価をリアルタイム取得
クレジット決済 Stripe APIでオンライン支払いを実現
Twitter(X)投稿 Pythonから自動でポスト
Instagram投稿 写真を自動でアップロード
Slack通知 研究室の進捗をSlackに自動送信
YouTube再生リスト取得 おすすめ動画一覧を取得して表示
音声合成 テキストをAIが声にして読み上げる
画像認識 写真から「猫」「人」などを判定
動画検索 YouTube APIで特定ジャンルの動画を検索
音楽再生 Spotify APIでプレイリストを取得して再生
ChatGPTでレポート添削 学生の文章をAIでチェック
翻訳APIで多言語教材 英語・中国語に自動翻訳して提示
Google Sheets API アンケート結果をスプレッドシートに自動記録
Python × Google Drive API 提出レポートを自動整理
arXiv API 最新の研究論文を自動取得して要約
スマート電球 Webから電気をオン/オフ
温度センサー 値をAPIでサーバーに送信し記録
Raspberry Pi IoTデバイスとAPIで通信して制御
LINE Bot連携 ボタンを押すと家のライトがつく

実例

chatGPTをpythonで使う

chatGPTのAPIは有料です。

ライブラリのインストール

ターミナルやコマンドプロンプトでchatGPTの会社OpenAIのライブラリをインストールします。

pip install openai

pythonでapiを使う

from openai import OpenAI

#APIを使うkeyを入力します。これは絶対にwebページなどで公開してはいけません。
client = OpenAI(api_key="sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")

question = input("AIに聞きたいことを入れてね:")

#ここがAPIです----------------------------
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": question}]
)
#---------------------------------------

print("AIの答え:", response.choices[0].message.content)

事例

学生が作った例

cookutte.png

Cookutteアプリ

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?