# 操弄 Heroku App 的環境變數
[TOC]
###### tags: `heroku`
---
## 認證/驗證
創建一個不會過期的 token <SUP>(令牌)</SUP>。
```shell
% heroku authorizations:create
Creating OAuth Authorization... !
heroku: Press any key to open up the browser to login or q to exit:
Opening browser to https://cli-auth.heroku.com/auth/cli/browser/…
Logging in... done
Logged in as someone@somewhere.net
Creating OAuth Authorization... done
Client: <none>
ID: fedcba98-7654-3210-fedc-ba9876543210
Description: Long-lived user authorization
Scope: global
Token: 01234567-89ab-cdef-0123-456789abcdef
Updated at: Wed Jan 12 2022 12:34:56 GMT+0800 (Taipei Standard Time) (less than a minute ago)
```
### 線上說明
:::info
```
create a new OAuth authorization
USAGE
$ heroku authorizations:create
OPTIONS
-S, --short only output token
-d, --description=description set a custom authorization description
-e, --expires-in=expires-in set expiration in seconds (default no expiration)
-j, --json output in json format
-s, --scope=scope set custom OAuth scopes
DESCRIPTION
This creates an authorization with access to your Heroku account.
```
:::
### 請求標頭
| 鍵 | 值 |
| --------------- | ---------------------------------------- |
| `Authorization` | `Bearer 令牌` |
| `Accept` | `application/vnd.heroku+json; version=3` |
## 上限
每小時 **4500** 次(或每分鐘 **75** 次)調用。
> Tokens are added to the account pool at a rate of roughly **75 per minute** (or **4500 per hour**), up to a **maximum of 4500**. If no tokens remain, further calls will return **429 Too Many Requests** until more tokens become available.
## 取得 App ID
```
GET https://api.heroku.com/apps/${appName} HTTP/1.1
Authorization: Bearer ${token}
Accept: application/vnd.heroku+json; version=3
```
- 其實此為取得 App 相關信息用,若已知 App ID 並將 `${appName}` 改為 `${appId}` 結果也是一樣的。
出處:[App Info](https://devcenter.heroku.com/articles/platform-api-reference#app-info)。
## 取得 App 的環境變數
```
GET https://api.heroku.com/apps/${appId}/config-vars HTTP/1.1
Authorization: Bearer ${token}
Accept: application/vnd.heroku+json; version=3
```
詳見 [Config Vars Info for App]([link](https://devcenter.heroku.com/articles/platform-api-reference#config-vars-info-for-app))。
## 更新(或添加、刪除) App 的環境變數
```
PATCH https://api.heroku.com/apps/${appId}/config-vars HTTP/1.1
Authorization: Bearer ${token}
Accept: application/vnd.heroku+json; version=3
Content-Type: application/json
\r\n
{"VARIABLE_NAME":"variableValue"}
```
- 若變數已存在,則會**更新**變數值。
- 若變數不存在,則會**添加**該變數。
- 若變數值為 `null`,則會**刪除**該變數。
- 不需將原舊(其它)變數帶入。
詳見 [Config Vars Update]([link](https://devcenter.heroku.com/articles/platform-api-reference#config-vars-update))。
## 參考資料
- [Getting Started with the Platform API: Authentication](https://devcenter.heroku.com/articles/platform-api-quickstart#authentication)
- [Platform API Reference](https://devcenter.heroku.com/articles/platform-api-reference)