API Providers

There are many services which offer a web API for accessing the Ethereum Blockchain. These Providers allow connecting to them, which simplifies development, since you do not need to run your own instance or cluster of Ethereum nodes.

However, this reliance on third-party services can reduce resilience, security and increase the amount of required trust. To mitigate these issues, it is recommended you use a Default Provider.

EtherscanProvider inherits BaseProvider

The EtherscanProvider is backed by a combination of the various Etherscan APIs.

new ethers.providers.EtherscanProvider( [ network = "homestead" , [ apiKey ] ] )

Create a new EtherscanProvider connected to network with the optional apiKey.

The network may be specified as a string for a common network name, a number for a common chain ID or a [Network Object]provider-(network).

If no apiKey is provided, a shared API key will be used, which may result in reduced performance and throttled requests. It is highly recommended for production, you register with Etherscan for your own API key.

Note: Default API keys

If no apiKey is provided, a shared API key will be used, which may result in reduced performance and throttled requests.

It is highly recommended for production, you register with Etherscan for your own API key.

Supported Networks

  • homestead - Homestead (Mainnet)
  • goerli - Görli (clique testnet)
  • sepolia - Sepolia (proof-of-authority testnet)
  • arbitrum - Arbitrum Optimistic L2
  • arbitrum-goerli - Arbitrum Optimistic L2 testnet
  • matic - Polgon mainnet
  • maticmum - Polgon testnet
  • optimism - Optimism Optimistic L2
  • optimism-goerli - Optimism Optimistic L2 testnet

Etherscan Examples
// Connect to mainnet (homestead) provider = new EtherscanProvider(); // Connect to goerli testnet (these are equivalent) provider = new EtherscanProvider("goerli"); provider = new EtherscanProvider(5); network = ethers.providers.getNetwork("goerli"); // { // chainId: 5, // ensAddress: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e', // name: 'goerli' // } provider = new EtherscanProvider(network); // Connect to mainnet (homestead) with an API key provider = new EtherscanProvider(null, apiKey); provider = new EtherscanProvider("homestead", apiKey);
provider.getHistory( address ) Array< History >

@TODO... Explain

InfuraProvider inherits UrlJsonRpcProvider

The InfuraProvider is backed by the popular INFURA Ethereum service.

new ethers.providers.InfuraProvider( [ network = "homestead" , [ apiKey ] ] )

Create a new InfuraProvider connected to network with the optional apiKey.

The network may be specified as a string for a common network name, a number for a common chain ID or a [Network Object]provider-(network).

The apiKey can be a string Project ID or an object with the properties projectId and projectSecret to specify a Project Secret which can be used on non-public sources (like on a server) to further secure your API access and quotas.

InfuraProvider.getWebSocketProvider( [ network [ , apiKey ] ] ) WebSocketProvider

Create a new WebSocketProvider using the INFURA web-socket endpoint to connect to network with the optional apiKey.

The network and apiKey are specified the same as the constructor.

Note: Default API keys

If no apiKey is provided, a shared API key will be used, which may result in reduced performance and throttled requests.

It is highly recommended for production, you register with INFURA for your own API key.

Supported Networks

  • homestead - Homestead (Mainnet)
  • goerli - Görli (clique testnet)
  • sepolia - Sepolia (proof-of-authority testnet)
  • arbitrum - Arbitrum Optimistic L2
  • arbitrum-goerli - Arbitrum Optimistic L2 testnet
  • matic - Polgon mainnet
  • maticmum - Polgon testnet
  • optimism - Optimism Optimistic L2
  • optimism-goerli - Optimism Optimistic L2 testnet

INFURA Examples
// Connect to mainnet (homestead) provider = new InfuraProvider(); // Connect to the goerli testnet // (see EtherscanProvider above for other network examples) provider = new InfuraProvider("goerli"); // Connect to mainnet with a Project ID (these are equivalent) provider = new InfuraProvider(null, projectId); provider = new InfuraProvider("homestead", projectId); // Connect to mainnet with a Project ID and Project Secret provider = new InfuraProvider("homestead", { projectId: projectId, projectSecret: projectSecret }); // Connect to the INFURA WebSocket endpoints with a WebSocketProvider provider = InfuraProvider.getWebSocketProvider()

AlchemyProvider inherits UrlJsonRpcProvider

The AlchemyProvider is backed by Alchemy.

new ethers.providers.AlchemyProvider( [ network = "homestead" , [ apiKey ] ] )

Create a new AlchemyProvider connected to network with the optional apiKey.

The network may be specified as a string for a common network name, a number for a common chain ID or a Network Object.

Note: Default API keys

If no apiKey is provided, a shared API key will be used, which may result in reduced performance and throttled requests.

It is highly recommended for production, you register with Alchemy for your own API key.

Supported Networks

  • homestead - Homestead (Mainnet)
  • goerli - Görli (clique testnet)
  • arbitrum - Arbitrum Optimistic L2
  • arbitrum-goerli - Arbitrum Optimistic L2 testnet
  • matic - Polgon mainnet
  • maticmum - Polgon testnet
  • optimism - Optimism Optimistic L2
  • optimism-goerli - Optimism Optimistic L2 testnet

Alchemy Examples
// Connect to mainnet (homestead) provider = new AlchemyProvider(); // Connect to the goerli testnet // (see EtherscanProvider above for other network examples) provider = new AlchemyProvider("goerli"); // Connect to mainnet with an API key (these are equivalent) provider = new AlchemyProvider(null, apiKey); provider = new AlchemyProvider("homestead", apiKey); // Connect to the Alchemy WebSocket endpoints with a WebSocketProvider provider = AlchemyProvider.getWebSocketProvider()

CloudflareProvider inherits UrlJsonRpcProvider

The CloudflareProvider is backed by the Cloudflare Ethereum Gateway.

new ethers.providers.CloudflareProvider( )

Create a new CloudflareProvider connected to mainnet (i.e. "homestead").

Supported Networks

  • homestead - Homestead (Mainnet)

Cloudflare Examples
// Connect to mainnet (homestead) provider = new CloudflareProvider();

PocketProvider inherits UrlJsonRpcProvider

The PocketProvider is backed by Pocket.

new ethers.providers.PocketProvider( [ network = "homestead" , [ apiKey ] ] )

Create a new PocketProvider connected to network with the optional apiKey.

The network may be specified as a string for a common network name, a number for a common chain ID or a Network Object.

Note: Default API keys

If no apiKey is provided, a shared API key will be used, which may result in reduced performance and throttled requests.

It is highly recommended for production, you register with Pocket for your own API key.

Supported Networks

  • homestead - Homestead (Mainnet)
  • goerli - Görli (clique testnet)
  • matic - Polgon mainnet
  • maticmum - Polgon testnet

Pocket Examples
// Connect to mainnet (homestead) provider = new PocketProvider(); // Connect to the goerli testnet // (see EtherscanProvider above for other network examples) provider = new PocketProvider("goerli"); // Connect to mainnet with an Application ID (these are equivalent) provider = new PocketProvider(null, applicationId); provider = new PocketProvider("homestead", applicationId); // Connect to mainnet with an application ID, application secret // and specify whether to use the load balances provider = new PocketProvider("homestead", { applicationId: applicationId, applicationSecretKey: applicationSecretKey, loadBalancer: loadBalancer // true or false });

AnkrProvider inherits UrlJsonRpcProvider

The AnkrProvider is backed by Ankr.

new ethers.providers.AnkrProvider( [ network = "homestead" , [ apiKey ] ] )

Create a new AnkrProvider connected to network with the optional apiKey.

The network may be specified as a string for a common network name, a number for a common chain ID or a Network Object.

Note: Default API keys

If no apiKey is provided, a shared API key will be used, which may result in reduced performance and throttled requests.

It is highly recommended for production, you register with Ankr for your own API key.

Supported Networks

  • homestead - Homestead (Mainnet)
  • matic - Polygon
  • arbitrum - Arbitrum (L2; optimistic roll-up)

Ankr Examples
// Connect to mainnet (homestead) provider = new AnkrProvider(); // Connect to polygont // (see EtherscanProvider above for other network examples) provider = new AnkrProvider("matic"); // Connect to mainnet with an API Key (these are equivalent) provider = new AnkrProvider(null, apiKey); provider = new AnkrProvider("homestead", apiKey);