モチベーション
せっかくPostmanでAPIテストをしているので、じゃぁ簡単なAPIをつくってみようかという話
POSTMAN Learning Center様様です。
参考文献
手順
PostmanにはAPI builderという機能があるらしい
(空っぽの)APIをつくる
Postman Desktop App の左ペーンよりAPIsを選択 → メニュー内のCreate an APIをクリックする
モーダルが表示されるので、基本情報を入力・選択しCreate APIボタンをクリックする
API名 | バージョン | スキーマ | スキーマのフォーマット |
---|---|---|---|
my-first-api | 1.0.0 | OpenAPI 3.0 | JSON |
![]() |
APIを定義する
APIを定義する。Postmanには、定義したAPIから自動でcollectionを生成する機能もあるみたい、すごい。
APIを編集する
作成したAPIの仕様は、Defineタブに記載するみたい。
今回はOpenAPI 3.0をスキーマとして設定したため、PostmanがサンプルのAPI仕様を作ってくれている。
編集したら、Saveボタンをクリックして保存する。
Defineタブにデフォルトで入力されるAPI仕様
{
"openapi": "3.0.0",
"info": {
"version": "1.0.0",
"title": "my-first-api",
"license": {
"name": "MIT"
}
},
"servers": [
{
"url": "http://petstore.swagger.io/v1"
}
],
"paths": {
"/user": {
"get": {
"summary": "Details about a user",
"operationId": "listUser",
"tags": [
"user"
],
"parameters": [
{
"name": "id",
"in": "query",
"description": "ID of the user",
"required": true,
"schema": {
"type": "integer",
"format": "int32"
}
}
],
"responses": {
"200": {
"description": "Details about a user",
"headers": {
"x-next": {
"description": "A link to the next page of responses",
"schema": {
"type": "string"
}
}
},
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/User"
}
}
}
},
"default": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"User": {
"type": "object",
"required": [
"id",
"name"
],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
}
},
"Error": {
"type": "object",
"required": [
"code",
"message"
],
"properties": {
"code": {
"type": "integer",
"format": "int32"
},
"message": {
"type": "string"
}
}
}
}
}
}
作成したAPIからcollectionを生成する
作成したAPIのドキュメントを書きたい、テストを書きたい、モックサーバーを書きたいという場合は、Generate Collection機能を使用する。
ウィンドウ右側にGenerrate Collectionボタンがあるのでクリックする:
モーダルが表示されるので、Collection名を入力し、用途を選択する:
Generate Collectionボタンをクリックすると、collectionが生成される。
今回は、用途として「Test the API」を選択した:
備考
モックサーバーをつくることはできるが、呼び出し回数には制限がある(無料プランだと、月間1,000回まで)。
現在の呼び出し回数は、Add-onsから確認できる:
https://web.postman.co/billing/add-ons/overview