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

# Execute function

> Run a function in a secure, isolated environment. Define a function named `execute`. The function will be passed `input` as an object.



## OpenAPI

````yaml post /v1/execute-function
openapi: 3.0.3
info:
  title: Execute
  version: 0.1.4
servers:
  - url: https://api.riza.io
security:
  - bearerHttpAuthentication: []
paths:
  /v1/execute-function:
    post:
      tags:
        - Sandbox
      summary: Execute function
      description: >-
        Run a function in a secure, isolated environment. Define a function
        named `execute`. The function will be passed `input` as an object.
      operationId: Sandbox_ExecuteFunction
      requestBody:
        content:
          application/json:
            examples:
              Python:
                value:
                  language: python
                  code: >-
                    def execute(input): return { "name": input["name"],
                    "executed": True }
                  input:
                    name: John
            schema:
              $ref: '#/components/schemas/ExecuteFunctionRequest'
        required: true
      responses:
        '200':
          content:
            application/json:
              examples:
                Python:
                  value:
                    output:
                      name: John
                      executed: true
                    output_status: valid
                    execution:
                      id: ''
                      exit_code: 0
                      stdout: ''
                      stderr: ''
                      duration: 7
              schema:
                $ref: '#/components/schemas/ExecuteFunctionResponse'
          description: OK
        '401':
          content:
            application/json:
              examples:
                unauthenticated:
                  value:
                    code: 16
                    message: unauthenticated
              schema:
                $ref: '#/components/schemas/Status'
          description: Unauthenticated error response
      x-codeSamples:
        - lang: JavaScript
          source: |-
            import Riza from '@riza-io/api';

            const client = new Riza({
              apiKey: process.env['RIZA_API_KEY'], // This is the default and can be omitted
            });

            const response = await client.command.execFunc({
              code: 'def execute(input): return { "name": input["name"], "executed": True }',
              language: 'python',
              input: { name: 'John' },
            });

            console.log(response.execution);
        - lang: Python
          source: |-
            import os
            from rizaio import Riza

            client = Riza(
                api_key=os.environ.get("RIZA_API_KEY"),  # This is the default and can be omitted
            )
            response = client.command.exec_func(
                code="def execute(input): return { \"name\": input[\"name\"], \"executed\": True }",
                language="python",
                input={
                    "name": "John"
                },
            )
            print(response.execution)
        - lang: Go
          source: |
            package main

            import (
              "context"
              "fmt"

              "github.com/riza-io/riza-api-go"
              "github.com/riza-io/riza-api-go/option"
            )

            func main() {
              client := riza.NewClient(
                option.WithAPIKey("My API Key"), // defaults to os.LookupEnv("RIZA_API_KEY")
              )
              response, err := client.Command.ExecFunc(context.TODO(), riza.CommandExecFuncParams{
                Code: riza.F(`def execute(input): return { "name": input["name"], "executed": True }`),
                Language: riza.F(riza.CommandExecFuncParamsLanguagePython),
                Input: riza.F[any](map[string]interface{}{
                "name": "John",
                }),
              })
              if err != nil {
                panic(err.Error())
              }
              fmt.Printf("%+v\n", response.Execution)
            }
components:
  schemas:
    ExecuteFunctionRequest:
      properties:
        code:
          description: >-
            The function to execute. Your code must define a function named
            "execute" that takes in a single argument and returns a
            JSON-serializable value.
          type: string
        env:
          additionalProperties:
            type: string
          description: >-
            Set of key-value pairs to add to the function's execution
            environment.
          type: object
        files:
          description: List of input files.
          items:
            $ref: '#/components/schemas/File'
          type: array
        http:
          $ref: '#/components/schemas/HTTP'
        input:
          description: >-
            The input to the function. This must be a valid JSON-serializable
            object. If you do not pass an input, your function will be called
            with None (Python) or null (JavaScript/TypeScript) as the argument.
        language:
          description: The interpreter to use when executing code.
          enum:
            - python
            - javascript
            - typescript
          type: string
        limits:
          $ref: '#/components/schemas/Limits'
        runtime_revision_id:
          description: The ID of the runtime revision to use when executing code.
          type: string
      required:
        - language
        - code
      type: object
    ExecuteFunctionResponse:
      properties:
        execution:
          $ref: '#/components/schemas/ExecutionResult'
        output:
          description: The output of the function.
        output_status:
          description: >-
            The status of the output. "valid" means your function executed
            successfully and returned a valid JSON-serializable object, or void.
            "json_serialization_error" means your function executed
            successfully, but returned a nonserializable object. "error" means
            your function failed to execute.
          enum:
            - error
            - json_serialization_error
            - valid
          type: string
      required:
        - output
        - output_status
        - execution
      type: object
    Status:
      properties:
        code:
          description: >-
            The status code, which should be an enum value of
            [google.rpc.Code][google.rpc.Code].
          type: integer
        message:
          description: >-
            A developer-facing error message, which should be in English. Any
            user-facing error message should be localized and sent in the
            [google.rpc.Status.details][google.rpc.Status.details] field, or
            localized by the client.
          type: string
      type: object
    File:
      properties:
        contents:
          description: The contents of the file.
          type: string
        path:
          description: The relative path of the file.
          type: string
      type: object
    HTTP:
      description: Configuration for HTTP requests and authentication.
      properties:
        allow:
          description: List of allowed HTTP hosts and associated authentication.
          items:
            $ref: '#/components/schemas/HTTPAllow'
          type: array
      type: object
    Limits:
      description: Configuration for execution environment limits.
      properties:
        execution_timeout:
          description: The maximum time allowed for execution (in seconds). Default is 30.
          type: integer
        memory_size:
          description: The maximum memory allowed for execution (in MiB). Default is 128.
          type: integer
      type: object
    ExecutionResult:
      description: The execution details of the function.
      properties:
        duration:
          description: The execution time of the function in milliseconds.
          type: integer
        exit_code:
          description: >-
            The exit code returned by the function. Will often be '0' on success
            and non-zero on failure.
          type: integer
        id:
          description: The ID of the execution.
          type: string
        stderr:
          description: The contents of 'stderr' after executing the function.
          type: string
        stdout:
          description: The contents of 'stdout' after executing the function.
          type: string
      required:
        - id
        - exit_code
        - stdout
        - stderr
        - duration
      type: object
    HTTPAllow:
      description: List of allowed HTTP hosts and associated authentication.
      properties:
        auth:
          $ref: '#/components/schemas/HTTPAuth'
        host:
          description: The hostname to allow.
          type: string
      type: object
    HTTPAuth:
      description: Authentication configuration for outbound requests to this host.
      properties:
        basic:
          $ref: '#/components/schemas/HTTPBasic'
        bearer:
          $ref: '#/components/schemas/HTTPBearer'
        header:
          $ref: '#/components/schemas/HTTPHeader'
        query:
          $ref: '#/components/schemas/HTTPQuery'
      type: object
    HTTPBasic:
      properties:
        password:
          type: string
        user_id:
          type: string
      type: object
    HTTPBearer:
      description: >-
        Configuration to add an 'Authorization' header using the 'Bearer'
        scheme.
      properties:
        token:
          description: 'The token to set, e.g. ''Authorization: Bearer <token>''.'
          type: string
      type: object
    HTTPHeader:
      properties:
        name:
          type: string
        value:
          type: string
      type: object
    HTTPQuery:
      properties:
        key:
          type: string
        value:
          type: string
      type: object
  securitySchemes:
    bearerHttpAuthentication:
      scheme: bearer
      type: http

````