> For the complete documentation index, see [llms.txt](https://docs.umbraco.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.umbraco.com/ai-in-umbraco/19.latest/reference/models/ai-connection.md).

# AIConnection

Connection to an AI provider with credentials.

Represents a connection to an AI provider with authentication settings.

## Namespace

{% code title="Namespace" %}

```csharp
using Umbraco.AI.Core.Connections;
```

{% endcode %}

## Class Definition

{% code title="AIConnection" %}

```csharp
public class AIConnection : IAIVersionableEntity
{
    public Guid Id { get; internal set; }
    public required string Alias { get; set; }
    public required string Name { get; set; }
    public required string ProviderId { get; init; }
    public object? Settings { get; set; }
    public bool IsActive { get; set; } = true;
    public DateTime DateCreated { get; init; } = DateTime.UtcNow;
    public DateTime DateModified { get; set; } = DateTime.UtcNow;
    public int Version { get; internal set; } = 1;
    public Guid? CreatedByUserId { get; set; }
    public Guid? ModifiedByUserId { get; set; }
}
```

{% endcode %}

## Properties

| Property           | Type       | Description                                            |
| ------------------ | ---------- | ------------------------------------------------------ |
| `Id`               | `Guid`     | Unique identifier (assigned on save)                   |
| `Alias`            | `string`   | Unique alias for lookups                               |
| `Name`             | `string`   | Display name                                           |
| `ProviderId`       | `string`   | ID of the provider (for example, `"openai"`)           |
| `Settings`         | `object?`  | Provider-specific settings                             |
| `IsActive`         | `bool`     | Whether connection is enabled                          |
| `DateCreated`      | `DateTime` | Creation timestamp (UTC)                               |
| `DateModified`     | `DateTime` | Last modification timestamp (UTC)                      |
| `Version`          | `int`      | Version number, starts at 1, increments with each save |
| `CreatedByUserId`  | `Guid?`    | ID of the user who created the connection              |
| `ModifiedByUserId` | `Guid?`    | ID of the user who last modified the connection        |

## Settings

Settings are provider-specific. Each provider defines its own settings class.

### OpenAI Example

{% code title="OpenAI Settings" %}

```csharp
public class OpenAIProviderSettings
{
    public required string ApiKey { get; set; }
    public string? OrganizationId { get; set; }
    public string? BaseUrl { get; set; }
}
```

{% endcode %}

### Configuration References

Settings values starting with `$` are resolved from configuration:

{% code title="Using Config References" %}

```csharp
var connection = new AIConnection
{
    Alias = "production-openai",
    Name = "Production OpenAI",
    ProviderId = "openai",
    Settings = new
    {
        ApiKey = "$Umbraco:AI:Secrets:OpenAIApiKey"  // Resolved from configuration
    }
};
```

{% endcode %}

{% code title="appsettings.json" %}

```json
{
    "Umbraco": {
        "AI": {
            "Secrets": {
                "OpenAIApiKey": "sk-actual-key-here"
            }
        }
    }
}
```

{% endcode %}

References resolve from the `Umbraco:AI:Secrets` and `Umbraco:AI:Variables` sections by default. See [Configuration References](/ai-in-umbraco/19.latest/reference/configuration/ai-options.md#configuration-references).

## Creating a Connection

{% code title="Example" %}

```csharp
using Umbraco.AI.Core.Connections;

var connection = new AIConnection
{
    Alias = "my-openai",
    Name = "My OpenAI Connection",
    ProviderId = "openai",
    Settings = new
    {
        ApiKey = "$Umbraco:AI:Secrets:OpenAIApiKey",
        OrganizationId = "org-123"
    },
    IsActive = true
};

var saved = await connectionService.SaveConnectionAsync(connection);
Console.WriteLine($"Connection ID: {saved.Id}");
```

{% endcode %}

## Notes

* `AIConnection` implements `IAIVersionableEntity` for version tracking
* `Id` is assigned automatically when saving
* `Version` starts at 1 and increments with each save
* `Alias` is mutable and can be changed after creation
* `ProviderId` is immutable after creation (`init` setter)
* `ProviderId` must match a registered provider
* Settings are validated against the provider's schema on save


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.umbraco.com/ai-in-umbraco/19.latest/reference/models/ai-connection.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
