Skip to content

Commit 121e11c

Browse files
committed
docs: add middleware docs
1 parent d0868c2 commit 121e11c

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

packages/network/README.md

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ npm install @stacks/network
1010

1111
## Usage
1212

13-
Creating a Stacks mainnet, testnet or mocknet network
13+
### Create a Stacks mainnet, testnet or mocknet network
1414

1515
```typescript
1616
import { StacksMainnet, StacksTestnet, StacksMocknet } from '@stacks/network';
@@ -22,19 +22,19 @@ const testnet = new StacksTestnet();
2222
const mocknet = new StacksMocknet();
2323
```
2424

25-
Setting a custom node URL
25+
### Set a custom node URL
2626

2727
```typescript
2828
const network = new StacksMainnet({ url: 'https://www.mystacksnode.com/' });
2929
```
3030

31-
Check if network is mainnet
31+
### Check if network is mainnet
3232

3333
```typescript
3434
const isMainnet = network.isMainnet();
3535
```
3636

37-
Example usage in transaction builder
37+
### Network usage in transaction building
3838

3939
```typescript
4040
import { makeSTXTokenTransfer } from '@stacks/transactions';
@@ -49,7 +49,29 @@ const txOptions = {
4949
const transaction = await makeSTXTokenTransfer(txOptions);
5050
```
5151

52-
Get various API URLs
52+
### Use the built-in API key middleware
53+
54+
Some Stacks APIs make use API keys to provide less rate-limited plans.
55+
56+
```typescript
57+
const myApiMiddleware = createApiKeyMiddleware('example_e8e044a3_41d8b0fe_3dd3988ef302');
58+
const myFetchFn = createFetchFn(apiMiddleware); // middlewares can be used to create a new fetch function
59+
const myMainnet = new StacksMainnet({ fetchFn: myFetchFn }); // the fetchFn options can be passed to a StacksNetwork to override the default fetch function
60+
61+
const txOptions = {
62+
recipient: 'SP3FGQ8Z7JY9BWYZ5WM53E0M9NK7WHJF0691NZ159',
63+
amount: 12345n,
64+
senderKey: 'b244296d5907de9864c0b0d51f98a13c52890be0404e83f273144cd5b9960eed01',
65+
memo: 'some memo',
66+
anchorMode: AnchorMode.Any,
67+
network: myMainnet, // make sure to pass in the custom network object
68+
};
69+
const transaction = await makeSTXTokenTransfer(txOptions); // fee-estimation will use the custom fetchFn
70+
71+
const response = await broadcastResponse(transaction, myMainnet); // make sure to broadcast via the custom network object
72+
```
73+
74+
### Get various API URLs
5375

5476
```typescript
5577
const txBroadcastUrl = network.getBroadcastApiUrl();

0 commit comments

Comments
 (0)