Variables

envless.variables.create()

Create one variable in an environment

def create(
    project: str,
    environment: str,
    body: VariableInput,
    *,
    product: str | None = None,
    timeout: float | None = None,
) -> VariableResource

Creates a single variable in an environment and returns the stored row.

value must already be ciphertext from encrypt_value. The server holds no key and will not encrypt for you, so a plaintext value is refused with 400 variable.ciphertext_invalid and error.field naming the offender. defaultValue obeys the same rule whenever you send it, and plaintext that reaches it is permanent, since it is copied into every version snapshot taken afterwards and into every rollback made from one.

The ciphertext check runs before the project and environment are resolved, so a malformed value aimed at a project that does not exist returns 400 and not 404. A name already taken by a live variable is 409 variable.name_conflict, and hitting the 1000 variable ceiling is 409 plan.limit_reached rather than a 402.

name must match ^[A-Za-z_][A-Za-z0-9_]*$ and be 1 to 100 characters: letters, digits and underscores only, never starting with a digit. A name that fails either rule is a 400, not a silent fix. Lowercase is legal. Envless stores and returns the name with exactly the casing you send and never upper-cases it for you, but uniqueness inside the environment is judged without regard to case, so creating api_key in an environment that already holds API_KEY is 409 variable.name_conflict rather than a second variable.

Example

import os

from envless import encrypt_value, envless

passphrase = os.environ['ENVLESS_PASSPHRASE']
workspace_id = envless.me.get()['workspaceId']

variable = envless.variables.create(
    'api',
    'production',
    {
        'name': 'STRIPE_SECRET_KEY',
        'value': encrypt_value(
            'sk_live_51H8xq', passphrase, workspace_id
        ),
        'required': True,
    },
)

print(variable['id'])

Parameters

ParameterTypeRequiredDescription
projectstrYesProject slug the environment belongs to.
environmentstrYesEnvironment slug to create the variable in.
body['name']strYesVariable name. Matches ^[A-Za-z_][A-Za-z0-9_]*$, 1 to 100 characters, unique among the live variables in the environment without regard to case.
body['value']strYesCiphertext from encrypt_value, 1 to 20000 characters and prefixed v1: or v2:.
body['type']strNoOne of the VARIABLE_TYPE values, defaults to string.
body['isSecret']boolNoMarks the variable as a secret, which the example feed reports as 'secret': True. Stored, but not echoed in the response.
body['visibility']strNoOne of the VARIABLE_VISIBILITY_LEVELS values, defaults to server.
body['required']boolNoMarks the variable as required for the environment, defaults to False.
body['defaultValue']str | NoneNoCiphertext fallback encrypted with the same workspace key, or None.
productstrNoProduct slug, needed when the project slug is not unique in the workspace.
timeoutfloatNoSeconds each attempt of this call may take, the response body included, before it raises EnvlessNetworkError with is_timeout set. Overrides the client's timeout for this call, and 0 turns the limit off.

Returns

VariableResource for the new variable, including the generated id you need for apply and the id routes.

Worth knowing

  • isSecret is accepted and stored but is not part of the response body.
  • create_many and apply run the same v1:/v2: prefix check, so plaintext is refused on every write route, not only this one.
  • Names are case reserved, not case normalised. Api_Key is stored as Api_Key, and no other casing of it can exist in the same environment.
  • A name is checked locally before anything is sent, so an unusable name never reaches the network.
  • A local refusal raises EnvlessApiError with code set to request.body_invalid and field set to 'name', the same vocabulary the API uses, but with request_id set to None because nothing was sent.

Required scopes

variables:write

Underlying endpoint

POST /projects/{slug}/environments/{environmentSlug}/variables

Request and response schema, every error code, and a live playground.

In TypeScript

envless.variables.create()

The same method in @goenvless/sdk, with the same request, retries and errors.