> ## AI Agent Context
> Get the complete documentation index at: https://aistudio.google.com/llms.txt
> Use this file to discover all available pages.
> For the full documentation in a single file, fetch: https://aistudio.google.com/llms-full.txt

# Creating an environment

{% callout type="note" %}
  This API is in Beta. Endpoints are under `/v1beta/`.

{% /callout %}

{% mark class="http-post" %}{% icon name="north_east" /%}`POST`{% /mark %} `https://generativelanguage.googleapis.com/v1beta/environments`

Creates an environment.

{% two-column-layout %}
  {% left-column %}
## Request Body  {% class="hide-from-toc" id="request-body" %}

  {% section class="api-ref-property-list" %}
  {% details %}
  {% summary %}
  **network**: `EnvironmentNetworkEgressAllowlist` or `string enum`

  Network configuration for the environment.

  {% /summary %}

  **Possible Types:** {% class="api-ref-label" %}

  {% details %}
  {% summary %}
  **EnvironmentNetworkEgressAllowlist**

  {% /summary %}

  Network egress configuration for the environment.

  {% details %}
  {% summary %}
  **allowlist**: `EgressRule array` or `string enum`

  List of allowed domains and their configurations. Set to `"disabled"` to block all network egress.

  {% /summary %}

  **Possible Types:** {% class="api-ref-label" %}

  {% details %}
  {% summary %}
  **EgressRule array**

  {% /summary %}

  A single domain allowlist rule with optional header injection.

  **domain**: {% mark %}required{% /mark %} `string`

  Domain to allow outbound requests to. Supports wildcards (e.g. '*.googleapis.com'). Use '*' to allow all domains.

  **transform**: `object`

  Headers to inject into requests matching this rule. Key: header name (e.g., "Authorization"). Value: header value (e.g., "Bearer your-token").

  **credential**: `string`

  Optional. Reference to a server-managed Credential resource by ID.

  {% /details %}

  {% details %}
  {% summary %}
  **string enum**

  {% /summary %}

  Possible values:

  - `disabled`: All network egress is blocked.

  {% /details %}

  {% /details %}

  {% /details %}

  {% details %}
  {% summary %}
  **string enum**

  {% /summary %}

  Possible values:

  - `disabled`: All network egress is blocked.

  {% /details %}

  {% /details %}

  {% hr /%}

  {% details %}
  {% summary %}
  **sources**: `Source array`

  Sources to be mounted into the environment.

  {% /summary %}

  {% details %}
  {% summary %}
  **type**: `string enum`

  {% /summary %}

  Possible values:

  - `gcs`: A Cloud Storage bucket.
  - `inline`: Inline content.
  - `repository`: A generic repository. The protocol prefix in the source URL identifies the provider (e.g., github://, gcs://).

  {% /details %}

  **source**: `string`

  The source of the environment. For Cloud Storage, this is the Cloud Storage path. For GitHub, this is the GitHub path.

  **target**: `string`

  Where the source should appear in the environment.

  **content**: `string`

  The inline content if `type` is `INLINE`.

  **encoding**: `string`

  Optional encoding for inline content (e.g. `base64`).

  {% /details %}

  {% hr /%}

  **from_environment**: `string`

  Optional. The source environment to copy/fork from. Format: `environments/{environment_id}` or `{environment_id}`. When specified, `sources` and `env` must be empty.

  {% /section %}

  {% /left-column %}

  {% right-column class="sticky" %}
  **Example request** {% class="example-heading" %}

  {% tabs %}
  {% tab label="Create" %}
  {% tabs %}
  {% tab label="REST" %}
  ```sh
  curl -X POST https://generativelanguage.googleapis.com/v1beta/environments \
    -H "x-goog-api-key: $GEMINI_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "sources": [{
        "type": "inline",
        "target": "main.py",
        "content": "print(\"Hello, World!\")"
      }]
    }'
  ```

  {% /tab %}

  {% tab label="Python" %}
  ```python
  from google import genai
  
  client = genai.Client()
  environment = client.environments.create(
      sources=[{
          "type": "inline",
          "target": "main.py",
          "content": "print('Hello, World!')",
      }]
  )
  print(environment.id)
  ```

  {% /tab %}

  {% tab label="JavaScript" %}
  ```javascript
  import {GoogleGenAI} from '@google/genai';
  
  const ai = new GoogleGenAI({});
  const environment = await ai.environments.create({
    sources: [
      {
        type: 'inline',
        target: 'main.py',
        content: "print('Hello, World!')",
      },
    ],
  });
  if (!environment.id) {
    throw new Error('Environment creation failed: ID is undefined');
  }
  console.log(environment.id);
  ```

  {% /tab %}

  {% tab label="Java" %}
  ```java
  import com.google.genai.Client;
  import com.google.genai.gaos.models.environments.CreateEnvironmentRequest;
  import com.google.genai.gaos.models.environments.Environment;
  import com.google.genai.gaos.models.interactions.Source;
  import com.google.genai.gaos.models.interactions.SourceType;
  
  Client client = new Client();
  CreateEnvironmentRequest request =
      CreateEnvironmentRequest.builder()
          .sources(
              List.of(
                  Source.builder()
                      .type(SourceType.INLINE)
                      .target("main.py")
                      .content("print('Hello, World!')")
                      .build()))
          .build();
  com.google.genai.gaos.models.environments.Environment environment =
      client.environments.createEnvironment(request).environment().orElseThrow();
  System.out.println(environment.id().orElse(""));
  ```

  {% /tab %}

  {% tab label="Go" %}
  ```go
  package main
  
  import (
      "context"
      "fmt"
      "log"
      "google.golang.org/genai"
      gaos_environments "google.golang.org/genai/interactions/models/environments"
      gaos_interactions "google.golang.org/genai/interactions/models/interactions"
      "google.golang.org/genai/interactions/models/operations"
  )
  
  func main() {
      ctx := context.Background()
      client, err := genai.NewClient(ctx, nil)
      if err != nil {
          log.Fatal(err)
      }
  
      req := operations.CreateEnvironmentRequest{
          Body: gaos_environments.CreateEnvironmentRequest{
              Sources: []gaos_interactions.Source{
                  {
                      Type:    genai.Ptr(gaos_interactions.SourceTypeInline),
                      Target:  genai.Ptr("main.py"),
                      Content: genai.Ptr("print('Hello, World!')"),
                  },
              },
          },
      }
  
      res, err := client.Environments.CreateEnvironment(ctx, req)
      if err != nil {
          log.Fatal(err)
      }
  
      if res.Environment != nil && res.Environment.ID != "" {
          fmt.Println(res.Environment.ID)
  
      }
  }
  ```

  {% /tab %}

  {% /tabs %}

  {% /tab %}

  {% tab label="Copy" %}
  {% tabs %}
  {% tab label="REST" %}
  ```sh
  curl -X POST https://generativelanguage.googleapis.com/v1beta/environments \
    -H "x-goog-api-key: $GEMINI_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "from_environment": "environments/env_abc123"
    }'
  ```

  {% /tab %}

  {% tab label="Python" %}
  ```python
  from google import genai
  
  client = genai.Client()
  
  
  environment = client.environments.create(
      from_environment=source_env.id,
  )
  print(environment.id)
  ```

  {% /tab %}

  {% tab label="JavaScript" %}
  ```javascript
  import {GoogleGenAI} from '@google/genai';
  
  const ai = new GoogleGenAI({});
  
  
  const environment = await ai.environments.create({
    from_environment: sourceEnv.id,
  });
  if (!environment.id) {
    throw new Error('Environment creation failed: ID is undefined');
  }
  console.log(environment.id);
  ```

  {% /tab %}

  {% tab label="Java" %}
  ```java
  import com.google.genai.Client;
  import com.google.genai.gaos.models.environments.CreateEnvironmentRequest;
  import com.google.genai.gaos.models.environments.Environment;
  
  Client client = new Client();
  
  
  CreateEnvironmentRequest request =
      CreateEnvironmentRequest.builder().fromEnvironment(sourceEnvId).build();
  com.google.genai.gaos.models.environments.Environment environment =
      client.environments.createEnvironment(request).environment().orElseThrow();
  System.out.println(environment.id().orElse(""));
  ```

  {% /tab %}

  {% tab label="Go" %}
  ```go
  package main
  
  import (
      "context"
      "fmt"
      "log"
      "google.golang.org/genai"
      gaos_environments "google.golang.org/genai/interactions/models/environments"
      "google.golang.org/genai/interactions/models/operations"
  )
  
  func main() {
      ctx := context.Background()
      client, err := genai.NewClient(ctx, nil)
      if err != nil {
          log.Fatal(err)
      }
  
  
      res, err := client.Environments.CreateEnvironment(ctx, operations.CreateEnvironmentRequest{
          Body: gaos_environments.CreateEnvironmentRequest{
              FromEnvironment: genai.Ptr(sourceEnvID),
          },
      })
      if err != nil {
          log.Fatal(err)
      }
  
      if res.Environment != nil && res.Environment.ID != "" {
          fmt.Println(res.Environment.ID)
  
      }
  }
  ```

  {% /tab %}

  {% /tabs %}

  {% /tab %}

  {% /tabs %}

  {% /right-column %}

{% /two-column-layout %}

## Response  {% class="hide-from-toc" id="response" %}

{% section class="api-ref-property-list" %}
  **id**: {% mark %}required{% /mark %} `string`

  Output only. The ID of the environment.

  {% hr /%}

  {% details %}
  {% summary %}
  **sources**: `Source array`

  Sources to be mounted into the environment.

  {% /summary %}

  {% details %}
  {% summary %}
  **type**: `string enum`

  {% /summary %}

  Possible values:

  - `gcs`: A Cloud Storage bucket.
  - `inline`: Inline content.
  - `repository`: A generic repository. The protocol prefix in the source URL identifies the provider (e.g., github://, gcs://).

  {% /details %}

  **source**: `string`

  The source of the environment. For Cloud Storage, this is the Cloud Storage path. For GitHub, this is the GitHub path.

  **target**: `string`

  Where the source should appear in the environment.

  **content**: `string`

  The inline content if `type` is `INLINE`.

  **encoding**: `string`

  Optional encoding for inline content (e.g. `base64`).

  {% /details %}

  {% hr /%}

  **created**: `string`

  Output only. The time at which the environment was created in ISO 8601 format (YYYY-MM-DDThh:mm:ssZ).

  {% hr /%}

  **updated**: `string`

  Output only. The time at which the environment was last updated in ISO 8601 format (YYYY-MM-DDThh:mm:ssZ).

  {% hr /%}

  **last_accessed**: `string`

  Output only. The time at which the environment was last accessed in ISO 8601 format (YYYY-MM-DDThh:mm:ssZ).

  {% hr /%}

  {% details %}
  {% summary %}
  **status**: `string enum`

  Output only. The status of the environment container.

  {% /summary %}

  Possible values:

  - `active`: Output only. The status of the environment container.
  - `expired`: Output only. The status of the environment container.

  {% /details %}

  {% hr /%}

  {% details %}
  {% summary %}
  **network**: `EnvironmentNetworkEgressAllowlist` or `string enum`

  Network configuration for the environment.

  {% /summary %}

  **Possible Types:** {% class="api-ref-label" %}

  {% details %}
  {% summary %}
  **EnvironmentNetworkEgressAllowlist**

  {% /summary %}

  Network egress configuration for the environment.

  {% details %}
  {% summary %}
  **allowlist**: `EgressRule array` or `string enum`

  List of allowed domains and their configurations. Set to `"disabled"` to block all network egress.

  {% /summary %}

  **Possible Types:** {% class="api-ref-label" %}

  {% details %}
  {% summary %}
  **EgressRule array**

  {% /summary %}

  A single domain allowlist rule with optional header injection.

  **domain**: {% mark %}required{% /mark %} `string`

  Domain to allow outbound requests to. Supports wildcards (e.g. '*.googleapis.com'). Use '*' to allow all domains.

  **transform**: `object`

  Headers to inject into requests matching this rule. Key: header name (e.g., "Authorization"). Value: header value (e.g., "Bearer your-token").

  **credential**: `string`

  Optional. Reference to a server-managed Credential resource by ID.

  {% /details %}

  {% details %}
  {% summary %}
  **string enum**

  {% /summary %}

  Possible values:

  - `disabled`: All network egress is blocked.

  {% /details %}

  {% /details %}

  {% /details %}

  {% details %}
  {% summary %}
  **string enum**

  {% /summary %}

  Possible values:

  - `disabled`: All network egress is blocked.

  {% /details %}

  {% /details %}

  {% hr /%}

  **file_count**: `string`

  Output only. The number of files in the environment, output only.

  {% hr /%}

  **size_bytes**: `string`

  Output only. The total size of the environment files in bytes, output only.

{% /section %}

