Getting started
Back to home
Getting started

Attach a database to the app

Write the database's internal connection string into a secret environment variable of its project (DATABASE_URL for PostgreSQL, REDIS_URL for Redis, or envVar) without returning it, so the password never passes through your client. HTTP 409 database_not_ready means the database has no connection details yet; variable_exists means the variable already holds a different value: send replace: true to overwrite it, which also needs configuration:write. Deploy or restart the project afterwards so the app reads the variable.

POST/api/customer/v1/projects/{projectId}/databases/{databaseId}/attach

Path parameters

NameValue
projectIdRequired resource ID from an earlier API response.
databaseIdRequired resource ID from an earlier API response.

Request body

Send JSON with Content-Type: application/json. Nested fields and accepted values are listed in the schema.

FieldTypeRequired
envVarstringNo
replacebooleanNo
Full request schema
{
  "type": "object",
  "properties": {
    "envVar": {
      "type": "string",
      "minLength": 1
    },
    "replace": {
      "type": "boolean"
    }
  },
  "required": [],
  "additionalProperties": false
}

Example request

Set HCODE_BASE_URL and HCODE_API_KEY as shown in Getting started. Replace resource variables with your own IDs.

curl --request POST "$HCODE_BASE_URL/api/customer/v1/projects/${PROJECT_ID}/databases/${DATABASE_ID}/attach" \
  --header "Authorization: Bearer $HCODE_API_KEY" \
  --header "Content-Type: application/json" \
  --data '{
  "envVar": "REPLACE_WITH_ENV_VAR"
}'

Response

The JSON shape is defined in the response schema.

Response schema
{
  "type": "object",
  "properties": {
    "success": {
      "type": "boolean"
    },
    "variable": {
      "type": "string"
    },
    "databaseId": {
      "type": "string"
    },
    "isSecret": {
      "type": "boolean"
    },
    "changed": {
      "type": "boolean"
    },
    "restartRequired": {
      "type": "boolean"
    },
    "operationId": {
      "type": "string"
    },
    "operations": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "status": {
            "type": "string"
          },
          "href": {
            "type": "string"
          }
        },
        "required": [
          "id",
          "status",
          "href"
        ]
      }
    }
  },
  "required": [
    "success",
    "variable",
    "databaseId"
  ],
  "additionalProperties": true
}

See errors and retries for failure responses.