> For the complete documentation index, see [llms.txt](https://ai.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ai.ortusbooks.com/advanced/reference/built-in-functions/aigateway.md).

# aiGateway

Resolves an IGateway instance by name — a gateway bx-ai ships in core, or one registered by an external gateway module.

Resolves an `IGateway` instance by name — a gateway bx-ai ships in core, or one registered by an external gateway module.

## Syntax

```javascript
aiGateway( name, options, register, module )
```

## Parameters

| Parameter  | Type      | Required | Default | Description                                                                                                   |
| ---------- | --------- | -------- | ------- | ------------------------------------------------------------------------------------------------------------- |
| `name`     | `string`  | ✅        | —       | A core gateway name (`mock`, `cli`, `http`), a name registered in `aiGatewayRegistry()`, or a full class path |
| `options`  | `struct`  | ❌        | `{}`    | Configuration passed to the gateway's `configure()`. Shape is gateway-specific                                |
| `register` | `boolean` | ❌        | `false` | Auto-register the constructed instance into `aiGatewayRegistry()`, same pattern as `aiAgent()`/`aiTool()`     |
| `module`   | `string`  | ❌        | `""`    | Namespaces the registry key when `register: true` (`name` or `name@module`)                                   |

## Returns

A configured `IGateway` instance.

## Throws

`GatewayNotSupported` when the name matches no core gateway, nothing is registered under it, and it is not an instantiable class path.

## Resolution Order

1. **Core gateways** — `mock`, `cli`, `http`
2. **Registered gateways** — anything a module put in `aiGatewayRegistry()`
3. **Class path** — the name is tried as a directly-instantiable class
4. Otherwise `GatewayNotSupported`

## Examples

### Core Gateways

```javascript
// Blocking CLI approval prompt
cli = aiGateway( "cli" )

// Signed, network-reachable webhooks
http = aiGateway( "http", {
    secret           : getSecret(),
    callbackUrl      : "https://ops.example.com/bxai-events",
    toleranceSeconds : 300,
    requestTTLSeconds: 900
} )

// In-memory gateway for tests
mock = aiGateway( "mock" )
```

### Presenting Approvals Through a Gateway

```javascript
import bxModules.bxai.models.middleware.core.HumanInTheLoopMiddleware;

agent = aiAgent(
    tools       : [ deployTool ],
    middleware  : [ new HumanInTheLoopMiddleware(
        toolsRequiringApproval: [ "deploy" ],
        gateway               : aiGateway( "http", { secret: getSecret() } )
    ) ],
    checkpointer: aiMemory( "cache" )
)
```

### An Externally-Registered Gateway

```javascript
// The module registers itself when it loads
aiGatewayRegistry().register( new MyPlatformGateway(), "bx-ai-gateway-myplatform" )

// Resolve it by name anywhere
gateway = aiGateway( "myplatform" )
```

### Auto-Registering on Construction

```javascript
// Equivalent to constructing the gateway, then calling aiGatewayRegistry().register()
gateway = aiGateway( name: "http", options: { secret: getSecret() }, register: true, module: "bx-ai-gateway-myplatform" )
```

### Checking Capabilities

```javascript
gateway = aiGateway( "http" )

if ( gateway.supports( "humanApproval" ) ) {
    // safe to present an approval request
}

gateway.getCapabilities()  // [ "inboundMessages", "outboundMessages", "humanApproval" ]
```

## Events

Fires `onGatewayCreate` with `{ gateway }` on every resolution path.

## Related

* [Gateways](/main-components/gateways.md)
* [Human-in-the-Loop](/main-components/human-in-the-loop.md)
* [aiGatewayRegistry()](/advanced/reference/built-in-functions/aigatewayregistry.md)


---

# 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://ai.ortusbooks.com/advanced/reference/built-in-functions/aigateway.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.
