# 8. Connect engine network providers.

#### Loading Engine Provider

The `loadEngineProvider` function is designed to initialize a provider for the specified test network within a TypeScript application. By default, it utilizes a local development provider but offers flexibility to switch to custom URLs for external providers.

**Parameters:**

* `providerUrl`: Optional. The provider URL to be used. Defaults to `http://127.0.0.1:8600`.

**Functionality**

1. **Configuration:** The function constructs a `FallbackProviderJsonConfig`, setting up the network with a default or custom provider URL.

**Usage**

Use this function to load a network provider by adapting to local or external resources with ease:

```typescript
// Load with the default local provider
await loadEngineProvider();

// Load with a specified provider URL
await loadEngineProvider("http://my-custom-provider:8600");
```

The `getProviderInfo` function aids by structuring the provider's necessary details like priority and performance metrics, ensuring seamless provider management.

```typescript
import {
  FallbackProviderJsonConfig,
  NETWORK_CONFIG,
  NetworkName,
} from "@railgun-community/shared-models";
import { loadProvider } from "@railgun-community/wallet";
import { TEST_NETWORK } from "./utils/0_constants";

// forked function, replace with your own provider
/**
 * Loads an Engine provider with the specified URL.
 *
 * This function initializes a provider for the test network using either:
 * 1. A local development provider (default: http://127.0.0.1:8600)
 * 2. External providers that can be uncommented and configured
 *
 * The provider is configured with fallback options, priorities, and weights
 * to ensure reliable connectivity.
 *
 * @param providerUrl - The URL of the primary provider, defaults to "http://127.0.0.1:8600"
 * @returns A Promise that resolves when the provider is successfully loaded
 *
 * @example
 * // Load with default local provider
 * await loadEngineProvider();
 *
 * // Load with custom provider URL
 * await loadEngineProvider("http://my-custom-provider:8600");
 */
export const loadEngineProvider = async (
  providerUrl = "http://127.0.0.1:8600"
) => {
  const TEST_PROVIDERS_JSON: FallbackProviderJsonConfig = {
    chainId: NETWORK_CONFIG[TEST_NETWORK].chain.id,
    providers: [
      // The following is an example of how to use a forked setup

      getProviderInfo(providerUrl),
      // These should be uncommented and filled in with your preferred providers.
      // The above is for using FORK only, do not mix them.
      //   {
      //     provider: "https://cloudflare-eth.com/",
      //     priority: 3,
      //     weight: 2,
      //     maxLogsPerBatch: 1,
      //   },
      //   {
      //     provider: "https://rpc.ankr.com/eth",
      //     priority: 2,
      //     weight: 2,
      //     maxLogsPerBatch: 1,
      //   },
    ],
  };

  const pollingInterval = 1000 * 60 * 5; // 5 min

  await loadProvider(TEST_PROVIDERS_JSON, TEST_NETWORK, pollingInterval);
};

export const getProviderInfo = (providerUrl: string) => {
  return {
    provider: providerUrl,
    priority: 3,
    weight: 2,
    maxLogsPerBatch: 1,
  };
};
```


---

# Agent Instructions: 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:

```
GET https://docs.railgun.org/developer-guide/wallet/getting-started/8.-connect-engine-network-providers..md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
