> For the complete documentation index, see [llms.txt](https://docs.stacks.co/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.stacks.co/reference/stacks.js/stacks-network/functions/createnetwork.md).

# createNetwork

Creates a customized `StacksNetwork` object from a base network. Supports configuring a custom API key, custom base URL, and custom fetch function. Returns a deep copy — the original network constants are never mutated.

***

### Usage

#### Basic usage

```ts
import { createNetwork } from '@stacks/network';

// From a network name string
const network = createNetwork('mainnet');

// From a network constant
import { STACKS_TESTNET } from '@stacks/network';
const network = createNetwork(STACKS_TESTNET);
```

#### With an API key

```ts
import { createNetwork } from '@stacks/network';

const network = createNetwork('testnet', 'my-hiro-api-key');
```

#### With an options object

```ts
import { createNetwork } from '@stacks/network';

const network = createNetwork({
  network: 'mainnet',
  apiKey: 'my-hiro-api-key',
});
```

#### With custom API key options

```ts
import { createNetwork } from '@stacks/network';

const network = createNetwork({
  network: 'mainnet',
  apiKey: 'my-api-key',
  host: /\.example\.com$/,       // default: /(.*)api(.*)(\.stacks\.co|\.hiro\.so)$/i
  httpHeader: 'x-custom-api-key', // default: 'x-api-key'
});
```

#### With custom client options

```ts
import { createNetwork, STACKS_TESTNET } from '@stacks/network';

const network = createNetwork({
  network: STACKS_TESTNET,
  client: {
    baseUrl: 'https://custom-api.example.com',
    fetch: customFetchFunction,
  },
});
```

#### Notes

* Always returns a deep copy of the base network — the original constants (`STACKS_MAINNET`, etc.) are never modified.
* When an `apiKey` is provided, it creates an API key middleware that attaches the key as an HTTP header to matching requests.
* The default host pattern for the API key middleware matches Hiro-hosted APIs (`*.stacks.co`, `*.hiro.so`).

[**Reference Link**](https://github.com/stx-labs/stacks.js/tree/main/packages/network/src/network.ts#L140)

***

### Signature

```ts
function createNetwork(network: StacksNetworkName | StacksNetwork): StacksNetwork;

function createNetwork(
  network: StacksNetworkName | StacksNetwork,
  apiKey: string
): StacksNetwork;

function createNetwork(options: {
  network: StacksNetworkName | StacksNetwork;
  client?: ClientOpts;
  apiKey?: string;
  host?: RegExp | string;
  httpHeader?: string;
}): StacksNetwork;
```

***

### Returns

`StacksNetwork`

A new `StacksNetwork` object with the specified customizations applied.

***

### Parameters

The function has three overloaded call signatures:

#### Overload 1: Network only

| Parameter | Type                                 | Description                |
| --------- | ------------------------------------ | -------------------------- |
| `network` | `StacksNetworkName \| StacksNetwork` | The base network to clone. |

#### Overload 2: Network + API key

| Parameter | Type                                 | Description                                           |
| --------- | ------------------------------------ | ----------------------------------------------------- |
| `network` | `StacksNetworkName \| StacksNetwork` | The base network to clone.                            |
| `apiKey`  | `string`                             | API key added as an HTTP header to matching requests. |

#### Overload 3: Options object

| Property     | Type                                 | Required | Description                                                                                       |
| ------------ | ------------------------------------ | -------- | ------------------------------------------------------------------------------------------------- |
| `network`    | `StacksNetworkName \| StacksNetwork` | Yes      | The base network to clone.                                                                        |
| `client`     | `ClientOpts`                         | No       | Custom client with `baseUrl` and/or `fetch`.                                                      |
| `apiKey`     | `string`                             | No       | API key for the middleware.                                                                       |
| `host`       | `RegExp \| string`                   | No       | Host pattern the API key header applies to. Default: `/(.*)api(.*)(\.stacks\.co\|\.hiro\.so)$/i`. |
| `httpHeader` | `string`                             | No       | HTTP header name for the API key. Default: `'x-api-key'`.                                         |


---

# 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.stacks.co/reference/stacks.js/stacks-network/functions/createnetwork.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.
