# Ethereum Provider API

Sender injects a global JavaScript API into websites visited by its users using the `window.sender.ethereum` provider object. This API allows websites to request users' Ethereum accounts, read data from blockchains the user is connected to, and suggest that the user sign messages and transactions.

### Properties <a href="#properties" id="properties"></a>

#### window\.sender.ethereum.isSender <a href="#windowethereumismetamask" id="windowethereumismetamask"></a>

This property is `true` if the user has Sender installed.

### Methods <a href="#properties" id="properties"></a>

#### eth\_requestAccounts <a href="#eth_requestaccounts" id="eth_requestaccounts"></a>

Requests that the user provide an Ethereum address to be identified by. Use this method to [access a user's accounts](https://docs.metamask.io/wallet/get-started/access-accounts). This method is specified by [EIP-1102](https://eips.ethereum.org/EIPS/eip-1102).

Example:

```javascript
// eth_requestAccounts code snippet
document.getElementById('connectButton', connect);

function connect() {
  sender.ethereum
    .request({ method: 'eth_requestAccounts' })
    .then(handleAccountsChanged)
    .catch((error) => {
      if (error.code === 4001) {
        // EIP-1193 userRejectedRequest error
        console.log('Please connect to MetaMask.');
      } else {
        console.error(error);
      }
    });
}
```

#### wallet\_getPermissions <a href="#wallet_getpermissions" id="wallet_getpermissions"></a>

Gets the caller's current [permissions](https://docs.metamask.io/wallet/#restricted-methods). This method returns an array of the caller's permission objects. If the caller has no permissions, the array is empty.

#### wallet\_requestPermissions <a href="#wallet_requestpermissions" id="wallet_requestpermissions"></a>

Requests [permissions](https://docs.metamask.io/wallet/#restricted-methods) from the user. The request causes a Sender popup to appear. You should only request permissions in response to a direct user action, such as a button click.

Example:

```javascript
// wallet_requestPermissions code snippet

document.getElementById('requestPermissionsButton', requestPermissions);

function requestPermissions() {
  sender.ethereum
    .request({
      method: 'wallet_requestPermissions',
      params: [{ eth_accounts: {} }],
    })
    .then((permissions) => {
      const accountsPermission = permissions.find(
        (permission) => permission.parentCapability === 'eth_accounts'
      );
      if (accountsPermission) {
        console.log('eth_accounts permission successfully requested!');
      }
    })
    .catch((error) => {
      if (error.code === 4001) {
        // EIP-1193 userRejectedRequest error
        console.log('Permissions needed to continue.');
      } else {
        console.error(error);
      }
    });
}
```

#### window\.sender.ethereum.request(args) <a href="#windowethereumrequestargs" id="windowethereumrequestargs"></a>

```javascript
// RequestArguments interface
interface RequestArguments {
  method: string;
  params?: unknown[] | object;
}

window.sender.ethereum.request(args: RequestArguments): Promise<unknown>;
```

Use this method to submit [RPC API](https://docs.metamask.io/wallet/reference/rpc-api) requests to Ethereum using Sender It returns a promise that resolves to the result of the RPC method call.

The following is an example of using `window.sender.ethereum.request(args)` to call [`eth_sendTransaction`](https://metamask.github.io/api-playground/api-documentation/#eth_sendTransaction):

```javascript
// Using window.sender.ethereum.request(args) to call eth_sendTransaction example
params: [
  {
    from: '0xb60e8dd61c5d32be8058bb8eb970870f07233155',
    to: '0xd46e8dd67c5d32be8058bb8eb970870f07244567',
    gas: '0x76c0', // 30400
    gasPrice: '0x9184e72a000', // 10000000000000
    value: '0x9184e72a', // 2441406250
    data:
      '0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675',
  },
];

sender.ethereum
  .request({ method: 'eth_sendTransaction', params })
  .then((result) => {
    // The result varies by RPC method.
    // For example, this method returns a transaction hash hexadecimal string upon success.
  })
  .catch((error) => {
    // If the request fails, the Promise rejects with an error.
  });
```

### Events <a href="#events" id="events"></a>

The Sender provider emits events using the Node.js [`EventEmitter`](https://nodejs.org/api/events.html) API. The following is an example of listening to the [`accountsChanged`](https://docs.metamask.io/wallet/#accountschanged) event. You should remove listeners once you're done listening to an event (for example, on component unmount in React).

```javascript
// handle accounts changed
function handleAccountsChanged(accounts) {
  // Handle new accounts, or lack thereof.
}

window.sender.ethereum.on('accountsChanged', handleAccountsChanged);

// Later
window.sender.ethereum.removeListener('accountsChanged', handleAccountsChanged);
```

The first argument of `window.sender.ethereum.removeListener` is the event name, and the second argument is a reference to the function passed to `window.sender.ethereum.on` for the event.

#### accountsChanged <a href="#accountschanged" id="accountschanged"></a>

```javascript
window.sender.ethereum.on('accountsChanged', handler: (accounts: Array<string>) => void);
```

The Sender provider emits this event when the return value of the [`eth_accounts`](https://metamask.github.io/api-playground/api-documentation/#eth_accounts) RPC method changes. `eth_accounts` returns either an empty array, or an array that contains the address of the most recently used account the caller is permitted to access. Callers are identified by their URL origin, which means that all sites with the same origin share the same permissions.

This means that the provider emits `accountsChanged` when the user's exposed account address changes. Listen to this event to [handle accounts](https://docs.metamask.io/wallet/get-started/access-accounts#handle-accounts).

#### chainChanged <a href="#chainchanged" id="chainchanged"></a>

```
window.sender.ethereum.on('chainChanged', handler: (chainId: string) => void);
```

The provider emits this event when the currently connected chain changes. Listen to this event to [detect a user's network](https://docs.metamask.io/wallet/get-started/detect-network).

#### connect <a href="#connect" id="connect"></a>

```
interface ConnectInfo {
  chainId: string;
}

window.sender.ethereum.on('connect', handler: (connectInfo: ConnectInfo) => void);
```

The provider emits this event when it's first able to submit RPC requests to a chain.

#### disconnect <a href="#disconnect" id="disconnect"></a>

```
window.sender.ethereum.on('disconnect', handler: (error: ProviderRpcError) => void);
```

The provider emits this event if it becomes unable to submit RPC requests to a chain. In general, this only happens due to network connectivity issues or some unforeseen error.

#### message <a href="#message" id="message"></a>

```
interface ProviderMessage {
  type: string;
  data: unknown;
}

window.sender.ethereum.on('message', handler: (message: ProviderMessage) => void);
```

The provider emits this event when it receives a message that the user should be notified of. The `type` property identifies the kind of message.
