0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PostmanでAPIをつくる

Posted at

モチベーション

せっかくPostmanでAPIテストをしているので、じゃぁ簡単なAPIをつくってみようかという話
POSTMAN Learning Center様様です。

参考文献

Using the API Builder

手順

PostmanにはAPI builderという機能があるらしい

(空っぽの)APIをつくる

Postman Desktop App の左ペーンよりAPIsを選択 → メニュー内のCreate an APIをクリックする
postman_create1.png
モーダルが表示されるので、基本情報を入力・選択しCreate APIボタンをクリックする

API名 バージョン スキーマ スキーマのフォーマット
my-first-api 1.0.0 OpenAPI 3.0 JSON
postman_create2.png

↓↓のような感じになったら、とりあえずOKだと思う:
postman_create3.png

APIを定義する

APIを定義する。Postmanには、定義したAPIから自動でcollectionを生成する機能もあるみたい、すごい。

APIを編集する

作成したAPIの仕様は、Defineタブに記載するみたい。
今回はOpenAPI 3.0をスキーマとして設定したため、PostmanがサンプルのAPI仕様を作ってくれている。
編集したら、Saveボタンをクリックして保存する。
postman_create4.png

Defineタブにデフォルトで入力されるAPI仕様
sample-spec.json
{
  "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ボタンがあるのでクリックする:
postman_create5.png
モーダルが表示されるので、Collection名を入力し、用途を選択する:
postman_create6.png
Generate Collectionボタンをクリックすると、collectionが生成される。
今回は、用途として「Test the API」を選択した:
postman_create7.png

備考

モックサーバーをつくることはできるが、呼び出し回数には制限がある(無料プランだと、月間1,000回まで)。
現在の呼び出し回数は、Add-onsから確認できる:
https://web.postman.co/billing/add-ons/overview

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?