> 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/17.latest/reference/models/ai-connection.md).

# AIConnection

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/17.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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
