---
url: >-
  https://game-engine-io.github.io/enginex-frontend-sdk/reference/api/@getenginex/sdk-next/config/functions/withEnginex.md
---
[Reference](../../../../index.md) / [@getenginex/sdk-next/config](../index.md) / withEnginex

# withEnginex()

## Call Signature

> **withEnginex**<`A`, `R`>(`config`, `options`): (...`args`) => `Promise`<[`WithEnginexConfig`](../type-aliases/WithEnginexConfig.md)<`Awaited`<`R`>>>

Adds to `next.config` the rewrite that sends `/api/:path*` (and the other `paths`) to the gateway, as the
players do by hand today. To use in `next.config.ts` (`@getenginex/sdk-next/config` is neither server
nor client code; it has an ESM and a CJS build).

* **Preserves** the app's rewrites, as an array or as `{ beforeFiles, afterFiles, fallback }`, from a sync
  or async function; the gateway's go after the app's (in `afterFiles`, in object format).
* **Does not duplicate:** an app rewrite without a condition with the same `source` wins, and applying it twice does not
  repeat anything. An app rewrite with `has`/`missing` on the same `source` does not cover all requests:
  the gateway's goes after it.
* **Composes** with other wrappers: accepts the config object or the function `(phase, ctx) => config` (also
  async), and returns the same format. It does not change the object it receives.

The `x-original-host` the gateway uses to find the operator comes from the app's `proxy.ts` (see the
`getEnginexServer` docs); this helper does not touch it.

### Type Parameters

#### A

`A` *extends* `unknown`\[]

#### R

`R` *extends* `object` | `Promise`<`object`>

### Parameters

#### config

(...`args`) => `R`

The Next config, or the function that returns it.

#### options

[`WithEnginexOptions`](../interfaces/WithEnginexOptions.md)

Gateway URL and paths.

### Returns

The config with `rewrites` replaced by an async function (or the function that returns it).

(...`args`) => `Promise`<[`WithEnginexConfig`](../type-aliases/WithEnginexConfig.md)<`Awaited`<`R`>>>

### Throws

`CONFIGURATION_ERROR`, at call time, without an absolute `gatewayUrl` or with a
`paths` item that does not start with `/`.

### Example

```ts
// next.config.ts
import type { NextConfig } from 'next'
import { withEnginex } from '@getenginex/sdk-next/config'

const nextConfig: NextConfig = { reactStrictMode: true }

export default withEnginex(nextConfig, {
  gatewayUrl: process.env.NEXT_PUBLIC_API_URL!,
  paths: ['/api/:path*', '/dev/:path*'],
})
```

## Call Signature

> **withEnginex**<`C`>(`config`, `options`): [`WithEnginexConfig`](../type-aliases/WithEnginexConfig.md)<`C`>

Adds to `next.config` the rewrite that sends `/api/:path*` (and the other `paths`) to the gateway, as the
players do by hand today. To use in `next.config.ts` (`@getenginex/sdk-next/config` is neither server
nor client code; it has an ESM and a CJS build).

* **Preserves** the app's rewrites, as an array or as `{ beforeFiles, afterFiles, fallback }`, from a sync
  or async function; the gateway's go after the app's (in `afterFiles`, in object format).
* **Does not duplicate:** an app rewrite without a condition with the same `source` wins, and applying it twice does not
  repeat anything. An app rewrite with `has`/`missing` on the same `source` does not cover all requests:
  the gateway's goes after it.
* **Composes** with other wrappers: accepts the config object or the function `(phase, ctx) => config` (also
  async), and returns the same format. It does not change the object it receives.

The `x-original-host` the gateway uses to find the operator comes from the app's `proxy.ts` (see the
`getEnginexServer` docs); this helper does not touch it.

### Type Parameters

#### C

`C` *extends* `object`

### Parameters

#### config

`C`

The Next config, or the function that returns it.

#### options

[`WithEnginexOptions`](../interfaces/WithEnginexOptions.md)

Gateway URL and paths.

### Returns

[`WithEnginexConfig`](../type-aliases/WithEnginexConfig.md)<`C`>

The config with `rewrites` replaced by an async function (or the function that returns it).

### Throws

`CONFIGURATION_ERROR`, at call time, without an absolute `gatewayUrl` or with a
`paths` item that does not start with `/`.

### Example

```ts
// next.config.ts
import type { NextConfig } from 'next'
import { withEnginex } from '@getenginex/sdk-next/config'

const nextConfig: NextConfig = { reactStrictMode: true }

export default withEnginex(nextConfig, {
  gatewayUrl: process.env.NEXT_PUBLIC_API_URL!,
  paths: ['/api/:path*', '/dev/:path*'],
})
```
