> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ionworks.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Add a custom variable to an existing model

> Append a new custom variable to a model's config (append-only).

Validates the expression, serializes it to JSON, and stores it.
Custom variables cannot be edited or deleted after creation because
evaluated values are persisted in simulation result files.



## OpenAPI

````yaml https://api.ionworks.com/openapi.json post /models/{model_id}/custom-variables
openapi: 3.1.0
info:
  title: FastAPI
  version: 0.1.0
servers:
  - url: https://api.ionworks.com
    description: Production
security: []
paths:
  /models/{model_id}/custom-variables:
    post:
      summary: Add a custom variable to an existing model
      description: |-
        Append a new custom variable to a model's config (append-only).

        Validates the expression, serializes it to JSON, and stores it.
        Custom variables cannot be edited or deleted after creation because
        evaluated values are persisted in simulation result files.
      operationId: add_custom_variable_models__model_id__custom_variables_post
      parameters:
        - name: model_id
          in: path
          required: true
          schema:
            type: string
            title: Model Id
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/AddCustomVariable'
      responses:
        '201':
          description: Successful Response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ModelWithConfig'
        '422':
          description: Validation Error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HTTPValidationError'
components:
  schemas:
    AddCustomVariable:
      properties:
        name:
          type: string
          minLength: 1
          title: Name
          description: Display name of the custom variable.
        expression:
          type: string
          title: Expression
          description: >-
            PyBaMM expression string (e.g. CoupledVariable('Voltage [V]') -
            Parameter('Offset')).
      type: object
      required:
        - name
        - expression
      title: AddCustomVariable
      description: Request body for adding a custom variable to an existing model.
    ModelWithConfig:
      properties:
        name:
          type: string
          title: Name
          description: The name of the model.
        description:
          anyOf:
            - type: string
            - type: 'null'
          title: Description
          description: An optional description for the model.
        pybamm_version:
          anyOf:
            - type: string
            - type: 'null'
          title: Pybamm Version
          description: >-
            PyBaMM version used for this model. If not provided, will be read
            from pybamm.__version__.
        chemistry:
          type: string
          enum:
            - lithium_ion
            - lithium_sulfur
            - ecm
            - generic
          title: Chemistry
          description: >-
            Model chemistry kind: 'lithium_ion' (default — assumes pybamm
            lithium-ion conventions), 'lithium_sulfur', 'ecm', or 'generic' (no
            chemistry-specific enrichment).
          default: lithium_ion
        simulation_settings:
          anyOf:
            - additionalProperties: true
              type: object
            - type: 'null'
          title: Simulation Settings
          description: >-
            Optional persistent simulation kwargs (var_pts / submesh_types /
            spatial_methods / geometry / solver) in the flat canonical form,
            re-applied at simulation time. None uses the model defaults.
        id:
          type: string
          title: Id
          description: The unique identifier for the model.
        user_id:
          type: string
          title: User Id
          description: The ID of the user who created the model.
        organization_id:
          type: string
          title: Organization Id
          description: The ID of the organization this model belongs to.
        created_by_email:
          anyOf:
            - type: string
            - type: 'null'
          title: Created By Email
          description: Email of the user who created the model.
        created_at:
          type: string
          format: date-time
          title: Created At
          description: Timestamp of when the model was created.
        updated_at:
          type: string
          format: date-time
          title: Updated At
          description: Timestamp of when the model was last updated.
        config:
          additionalProperties: true
          type: object
          title: Config
          description: Model configuration stored as JSON.
        is_custom_model:
          type: boolean
          title: Is Custom Model
          description: Check if this is a custom model (has compressed model data).
          readOnly: true
      type: object
      required:
        - name
        - id
        - user_id
        - organization_id
        - created_at
        - updated_at
        - config
        - is_custom_model
      title: ModelWithConfig
      description: Model with config loaded from database.
    HTTPValidationError:
      properties:
        detail:
          items:
            $ref: '#/components/schemas/ValidationError'
          type: array
          title: Detail
      type: object
      title: HTTPValidationError
    ValidationError:
      properties:
        loc:
          items:
            anyOf:
              - type: string
              - type: integer
          type: array
          title: Location
        msg:
          type: string
          title: Message
        type:
          type: string
          title: Error Type
        input:
          title: Input
        ctx:
          type: object
          title: Context
      type: object
      required:
        - loc
        - msg
        - type
      title: ValidationError

````