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

# Execute code

> Executes the given code snippet and returns the output. Without a session_id, a new session is created to run the code. If you pass a valid session_id, the code runs in that session. This is useful for running multiple code snippets in the same environment, because dependencies and similar things are persisted
between calls to the same session.




## OpenAPI

````yaml POST /tci/execute
openapi: 3.1.0
info:
  title: Together APIs
  description: The Together REST API. See https://docs.together.ai for more details.
  version: 2.0.0
  termsOfService: https://www.together.ai/terms-of-service
  contact:
    name: Together Support
    url: https://www.together.ai/contact
  license:
    name: MIT
    url: https://github.com/togethercomputer/openapi/blob/main/LICENSE
servers:
  - url: https://api.together.ai/v1
security:
  - bearerAuth: []
paths:
  /tci/execute:
    post:
      tags:
        - Code Interpreter
      summary: Execute code
      description: >
        Executes the given code snippet and returns the output. Without a
        session_id, a new session is created to run the code. If you pass a
        valid session_id, the code runs in that session. This is useful for
        running multiple code snippets in the same environment, because
        dependencies and similar things are persisted

        between calls to the same session.
      operationId: tci/execute
      parameters: []
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ExecuteRequest'
        description: Execute Request
        required: false
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ExecuteResponse'
          description: Execute Response
      callbacks: {}
      x-codeSamples:
        - lang: Python
          label: Together AI SDK (v2)
          source: |
            # Docs for v1 can be found by changing the above selector ^
            from together import Together
            import os

            client = Together(
                api_key=os.environ.get("TOGETHER_API_KEY"),
            )

            response = client.code_interpreter.execute(
                code="print('Hello world!')",
                language="python",
            )

            print(response.data.outputs[0].data);
        - lang: Python
          label: Together AI SDK (v1)
          source: |
            from together import Together
            import os

            client = Together(
                api_key=os.environ.get("TOGETHER_API_KEY"),
            )

            response = client.code_interpreter.run(
                code="print('Hello world!')",
                language="python",
            )

            print(response.data.outputs[0].data);
        - lang: TypeScript
          label: Together AI SDK (TypeScript)
          source: |
            import Together from "together-ai";

            const client = new Together({
              apiKey: process.env.TOGETHER_API_KEY,
            });

            const response = await client.codeInterpreter.execute({
              code: "print('Hello world!')",
              language: "python"
            });

            console.log(response.data?.outputs?.[0]?.data);
        - lang: JavaScript
          label: Together AI SDK (JavaScript)
          source: |
            import Together from "together-ai";

            const client = new Together({
              apiKey: process.env.TOGETHER_API_KEY,
            });

            const response = await client.codeInterpreter.execute({
              code: "print('Hello world!')",
              language: "python"
            });

            console.log(response.data?.outputs?.[0]?.data);
        - lang: Shell
          label: cURL
          source: |
            curl -X POST "https://api.together.ai/v1/tci/execute" \
                 -H "Authorization: Bearer $TOGETHER_API_KEY" \
                 -H "Content-Type: application/json" \
                 -d '{
                   "code": "print(\'Hello world!\')",
                   "language": "python"
                 }'
components:
  schemas:
    ExecuteRequest:
      title: ExecuteRequest
      required:
        - language
        - code
      properties:
        code:
          description: Code snippet to execute.
          example: print('Hello, world!')
          type: string
        files:
          description: >-
            Files to upload to the session. If present, files are uploaded
            before executing the given code.
          items:
            properties:
              content:
                type: string
              encoding:
                description: >-
                  Encoding of the file content. Use `string` for text files such
                  as code, and `base64` for binary files, such as images.
                enum:
                  - string
                  - base64
                type: string
              name:
                type: string
            required:
              - name
              - encoding
              - content
            type: object
          type: array
        language:
          default: python
          description: >-
            Programming language for the code to execute. Currently only
            supports Python.
          enum:
            - python
        session_id:
          description: >-
            Identifier of the current session. Used to make follow-up calls.
            Returns an error if the session does not belong to the caller or has
            expired.
          example: ses_abcDEF123
          nullable: false
          type: string
    ExecuteResponse:
      title: ExecuteResponse
      type: object
      description: >-
        The result of the execution. If successful, `data` contains the result
        and `errors` is null. If unsuccessful, `data` is null and `errors`
        contains the errors.
      oneOf:
        - title: SuccessfulExecution
          type: object
          required:
            - data
            - errors
          properties:
            errors:
              type: 'null'
            data:
              type: object
              nullable: false
              required:
                - session_id
                - outputs
              properties:
                outputs:
                  type: array
                  items:
                    discriminator:
                      propertyName: type
                    oneOf:
                      - title: StreamOutput
                        description: Outputs that were printed to stdout or stderr
                        type: object
                        required:
                          - type
                          - data
                        properties:
                          type:
                            enum:
                              - stdout
                              - stderr
                            type: string
                          data:
                            type: string
                      - title: ErrorOutput
                        description: >-
                          Errors and exceptions that occurred. If this output
                          type is present, your code did not execute
                          successfully.
                        properties:
                          data:
                            type: string
                          type:
                            enum:
                              - error
                            type: string
                        required:
                          - type
                          - data
                      - properties:
                          data:
                            properties:
                              application/geo+json:
                                type: object
                                additionalProperties: true
                              application/javascript:
                                type: string
                              application/json:
                                type: object
                                additionalProperties: true
                              application/pdf:
                                format: byte
                                type: string
                              application/vnd.vega.v5+json:
                                type: object
                                additionalProperties: true
                              application/vnd.vegalite.v4+json:
                                type: object
                                additionalProperties: true
                              image/gif:
                                format: byte
                                type: string
                              image/jpeg:
                                format: byte
                                type: string
                              image/png:
                                format: byte
                                type: string
                              image/svg+xml:
                                type: string
                              text/html:
                                type: string
                              text/latex:
                                type: string
                              text/markdown:
                                type: string
                              text/plain:
                                type: string
                            type: object
                          type:
                            enum:
                              - display_data
                              - execute_result
                            type: string
                        required:
                          - type
                          - data
                        title: DisplayorExecuteOutput
                    title: InterpreterOutput
                session_id:
                  type: string
                  description: >-
                    Identifier of the current session. Used to make follow-up
                    calls.
                  example: ses_abcDEF123
                  nullable: false
                status:
                  type: string
                  enum:
                    - success
                  description: Status of the execution. Currently only supports success.
        - title: FailedExecution
          type: object
          required:
            - data
            - errors
          properties:
            data:
              type: 'null'
            errors:
              type: array
              items:
                oneOf:
                  - type: string
                  - type: object
                    additionalProperties: true
                title: Error
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      x-bearer-format: bearer
      x-default: default

````