4.2 KiB
http
The http Transport connects to a JSON-RPC API via HTTP. Wraps Viem's http Transport.
Import
import { http } from '{{packageName}}'
Usage
import {
createConfig,
http // [!code hl]
} from '{{packageName}}'
import { mainnet, sepolia } from '{{packageName}}/chains'
export const config = createConfig({
chains: [mainnet, sepolia],
connectors: [injected()],
transports: {
[mainnet.id]: http('https://foo-bar-baz.quiknode.pro/...'), // [!code hl]
[sepolia.id]: http('https://foo-bar-sep.quiknode.pro/...'), // [!code hl]
},
})
::: warning If no URL is provided, then the transport will fall back to a public RPC URL on the chain. It is highly recommended to provide an authenticated RPC URL to prevent rate-limiting. :::
Batch JSON-RPC
The http Transport supports Batch JSON-RPC. This means that multiple JSON-RPC requests can be sent in a single HTTP request.
The Transport will batch up Actions over a given period and execute them in a single Batch JSON-RPC HTTP request. By default, this period is a zero delay meaning that the batch request will be executed at the end of the current JavaScript message queue. Consumers can specify a custom time period wait (in ms).
You can enable Batch JSON-RPC by setting the batch flag to true:
const transport = http('https://foo-bar-baz.quiknode.pro/...', {
batch: true // [!code hl]
})
Parameters
url
string
URL of the JSON-RPC API. Defaults to chain.rpcUrls.default.http[0].
const transport = http('https://foo-bar-baz.quiknode.pro/...')
batch
boolean | BatchOptions
Toggle to enable Batch JSON-RPC. Defaults to false
const transport = http('https://foo-bar-baz.quiknode.pro/...', {
batch: true // [!code focus]
})
batch.batchSize
number
The maximum number of JSON-RPC requests to send in a batch. Defaults to 1_000.
const transport = http('https://foo-bar-baz.quiknode.pro/...', {
batch: {
batchSize: 2_000 // [!code focus]
}
})
batch.wait
number
The maximum number of milliseconds to wait before sending a batch. Defaults to 0 (zero delay).
const transport = http('https://foo-bar-baz.quiknode.pro/...', {
batch: {
wait: 16 // [!code focus]
}
})
fetchOptions
Fetch options to pass to the internal fetch function. Useful for passing auth headers or cache options.
const transport = http('https://foo-bar-baz.quiknode.pro/...', {
fetchOptions: { // [!code focus:5]
headers: {
'Authorization': 'Bearer ...'
}
}
})
key
string
A key for the Transport. Defaults to "http".
const transport = http('https://foo-bar-baz.quiknode.pro/...', {
key: 'alchemy', // [!code focus]
})
name
string
A name for the Transport. Defaults to "HTTP JSON-RPC".
const transport = http('https://foo-bar-baz.quiknode.pro/...', {
name: 'Alchemy HTTP Provider', // [!code focus]
})
retryCount
number
The max number of times to retry when a request fails. Defaults to 3.
const transport = http('https://foo-bar-baz.quiknode.pro/...', {
retryCount: 5, // [!code focus]
})
retryDelay
number
The base delay (in ms) between retries. By default, the Transport will use exponential backoff (~~(1 << count) * retryDelay), which means the time between retries is not constant.
const transport = http('https://foo-bar-baz.quiknode.pro/...', {
retryDelay: 100, // [!code focus]
})
timeout
number
The timeout for requests. Defaults to 10_000.
const transport = http('https://foo-bar-baz.quiknode.pro/...', {
timeout: 60_000, // [!code focus]
})