建立 API

本頁說明如何建立 API。

必要條件

建立 API 前,請先完成下列步驟:

API ID 規定

許多顯示的 gcloud 指令都需要您指定 API 的 ID,格式為:API_ID。 API Gateway 會對 API ID 強制實行下列規定:

  • 長度上限為 63 個字元。
  • 只能使用小寫英文字母、數字或破折號。
  • 開頭不得為破折號。
  • 不得含有底線。

建立 API

建立 API。

Google Cloud 控制台

按照「將 API 部署至閘道」一文的說明,建立及部署 API。

Google Cloud CLI

如要建立 API,請參閱下列章節:

建立 API 的步驟

  1. 請利用下列指令傳回的專案 ID 進行驗證,以確保系統沒有將服務建立在錯誤的專案中。

    gcloud config list project

    如要變更預設專案,請執行下列指令,並將 PROJECT_ID 改成要建立服務的專案 ID: Google Cloud

    gcloud config set project PROJECT_ID

    如果專案 ID 包含冒號 (:),請參閱「網域範圍專案」,進一步瞭解如何在專案中建立 API。

  2. 查看 apis create 指令的說明:

    gcloud api-gateway apis create --help
  3. 執行下列指令來建立 API:

    gcloud api-gateway apis create API_ID

    其中:

    • API_ID 會指定新 API 的 ID。如需 API 命名指南,請參閱「API ID 規定」。

    API Gateway 在建立 API 時,會將資訊輸出至終端機。

  4. 成功完成後,您可以使用下列指令查看新 API 的詳細資料:

    gcloud api-gateway apis describe API_ID 

    這項指令會傳回下列內容:

          createTime: '2020-02-29T21:52:20.297426875Z'
          displayName: API_ID
          managedService: MANAGED_SERVICE_NAME.apigateway.PROJECT_ID.cloud.goog
          name: projects/PROJECT_ID/locations/global/apis/API_ID
          state: ACTIVE
          updateTime: '2020-02-29T21:52:20.647923711Z'

Google Cloud CLI 選項

Google Cloud CLI 支援許多選項,包括 gcloud 參考資料中所述的選項。此外,建立 API 時,您可以為 API Gateway 設定下列選項:

  • --async:立即將控制權傳回終端機,不必等待作業完成。
  • --display-name=NAME:指定 API 的顯示名稱,也就是 UI 中顯示的名稱。名稱不得包含空格。 請改用連字號和底線。預設值為 API_ID
  • --labels=KEY1=VALUE1,KEY2=VALUE2,...: 指定與 API 設定相關聯的標籤。
  • --managed-service:指定 API 的現有代管服務。另請參閱網域範圍專案

網域範圍專案

如果專案的範圍是您的網域,則專案 ID 會包含後面加入冒號 (:) 的網域名稱,例如 example.com:my-project。在網域範圍專案中建立 API 時,必須提供有效的代管服務。

如果使用 gcloud,請透過 --managed-service 旗標提供受管理服務名稱,如「Google Cloud CLI 選項」一文所述。

列出 API

列出專案中的所有 API。

Google Cloud 控制台

前往 Google Cloud 控制台的「API Gateway」頁面。

前往 API Gateway

Google Cloud CLI

gcloud api-gateway apis list 

這項指令會傳回下列格式的輸出內容:

  NAME                                              DISPLAY_NAME  MANAGED_SERVICE                          STATE
  projects/PROJECT_ID/locations/global/apis/API_ID  API_ID        MANAGED_SERVICE_NAME.apigateway.PROJECT_ID.cloud.goog  ACTIVE

使用 API ID 取得 API 的詳細資訊:

gcloud api-gateway apis describe API_ID 

更新 API

建立 API 後,您可以更新現有 API 的標籤和顯示名稱。

Google Cloud 控制台

  1. 前往 Google Cloud 控制台的「API Gateway」頁面。

    前往 API Gateway

  2. 按一下必要的 API。
  3. 點按「Details」(詳細資料) 分頁標籤。
  4. 按一下「編輯」
  5. 編輯「顯示名稱」或「標籤」,然後按一下「儲存」

Google Cloud CLI

使用下列 gcloud 選項更新現有 API:

  • --display-name
  • --update-labels
  • --clear-labels
  • --remove-labels

例如:

  gcloud api-gateway apis update API_ID \
    --update-labels=a=1,b=2

使用下列指令查看所有更新選項:

gcloud api-gateway apis update --help

刪除 API

如要刪除 API,請完成下列步驟:

  • 刪除與 API 及其 API 設定相關聯的 API Gateway。
  • 刪除與 API 相關聯的 API 設定。
  • 刪除 API。

Google Cloud 控制台

  1. 前往 Google Cloud 控制台的「API Gateway」頁面。

    前往 API Gateway

  2. 按一下要刪除的 API。
  3. 按一下「Configs」分頁標籤。
  4. 選取 API 設定,然後按一下「更多」 ,然後按一下「刪除」
  5. 按一下 返回 API Gateway 首頁。
  6. 依序按一下「更多」圖示 和「刪除」,即可刪除所選 API。

Google Cloud CLI

  1. 找出與 API 相關聯的 API Gateway 清單:

    gcloud api-gateway gateways list --filter='apiConfig:"projects/PROJECT_ID/locations/global/apis/API_ID"'
    
  2. 刪除與該 API 相關聯的每項 API 設定:

    gcloud api-gateway api-configs delete CONFIG_ID --api=API_ID 
  3. 刪除 API:

    gcloud api-gateway apis delete API_ID 

後續步驟