Upsert Environment Variable
Create or update an environment variable by its full name. If that name already exists, update its type and value; otherwise, create a new variable.
The name identifies the variable. This API does not rename variables: submitting a different name operates on a different variable and does not rename the original one.
Request Method
POST
Endpoint
https://api-${endpoint}.gptbots.ai/v1/env-variable/upsert
Authentication
Refer to API Overview for authentication instructions. Use the current Agent’s API key; no Agent ID, organization ID, or conversation_id is required.
Request
Example Request
curl -X POST 'https://api-${endpoint}.gptbots.ai/v1/env-variable/upsert' \
-H 'Authorization: Bearer ${API Key}' \
-H 'Content-Type: application/json' \
-d '{
"name": "env_brand",
"type": "STRING",
"value": "GPTBots"
}'
Request Headers
| Field | Type | Description |
|---|---|---|
| Authorization | Bearer ${API Key} | Use Authorization: Bearer ${API Key} for authentication. Obtain the key from the Agent’s API Key page. |
| Content-Type | application/json | Fixed value: application/json. |
Request Parameters
| Field | Type | Required | Description |
|---|---|---|---|
| name | string | Yes | Full name starting with env_, such as env_brand. Only lowercase letters, digits, and underscores are allowed, and the name must start with a letter. A bare name such as brand is rejected rather than prefixed automatically. A repeated env_env_ prefix is not allowed. |
| type | string | Yes | One of STRING, INTEGER, NUMBER, BOOLEAN, or DATETIME. Updates to an existing name may change its type. Include the current type even when only updating the value. |
| value | string | No | Value in the format described below. Omitting this field or passing null clears the existing value rather than preserving it. |
Value Formats
| type | value format | Example |
|---|---|---|
| STRING | String. | "GPTBots" |
| INTEGER | Integer string. | "3" |
| NUMBER | Numeric string. | "88.8888" |
| BOOLEAN | The string "true" or "false", not a JSON boolean. |
"false" |
| DATETIME | Unix epoch timestamp in milliseconds, passed as a string, not a timestamp in seconds. | "1767225600000" |
Update Behavior
- When changing the type and supplying a new
value, both the new type and the supplied value are saved. Omittingvalueor passingnullclears the old value, even if the type is unchanged. - Each Agent can have up to 50 environment variables, counted separately from conversation properties. Existing variables can still be updated at the limit; creating a 51st variable fails.
- Subsequent reads by this Agent use the saved value. A message already being executed may continue using a value read earlier; historical messages and previously loaded text are not rewritten.
- Conversation property APIs cannot override environment variables.
GET /v1/conversation/propertyexcludes them, andPOST /v1/conversation/property/updatedoes not write them.
Change the Type
An existing variable can be changed from STRING to NUMBER, saving the numeric string supplied in this request:
curl -X POST 'https://api-${endpoint}.gptbots.ai/v1/env-variable/upsert' \
-H 'Authorization: Bearer ${API Key}' \
-H 'Content-Type: application/json' \
-d '{
"name": "env_brand",
"type": "NUMBER",
"value": "88.8888"
}'
Clear the Value
Keep the variable’s name and type, and omit value to clear the existing value:
curl -X POST 'https://api-${endpoint}.gptbots.ai/v1/env-variable/upsert' \
-H 'Authorization: Bearer ${API Key}' \
-H 'Content-Type: application/json' \
-d '{
"name": "env_brand",
"type": "NUMBER"
}'
Response
Example Response
{
"name": "env_brand",
"type": "STRING",
"value": "GPTBots"
}
The response is the saved object without an outer
datawrapper. The API does not expose the variable’s internal ID.
Successful Response
| Field | Type | Description |
|---|---|---|
| name | string | Full name, such as env_brand. Use this full name in references. |
| type | string | STRING, INTEGER, NUMBER, BOOLEAN, or DATETIME. |
| value | string | Global value, returned as a string when set. Treat null or an absent field as unset. BOOLEAN uses "true"/"false", and DATETIME uses an epoch-millisecond string. |
Error Response
| Field | Type | Description |
|---|---|---|
| code | int | Error code. |
| message | string | Error details. |
Error Codes
| Code | Message |
|---|---|
| 40000 | Invalid parameters: missing required fields, invalid name format or prefix, unsupported type, or the 50-variable limit reached when creating a variable. |
| 40001 | Request frequency exceeded. Retry later. |
| 40127 | Developer authentication failed. |
| 20029 | Name conflict. |
| 20059 | Agent has been deleted. |
