@@ -10,7 +10,7 @@ npm install @stacks/network
10
10
11
11
## Usage
12
12
13
- Creating a Stacks mainnet, testnet or mocknet network
13
+ ### Create a Stacks mainnet, testnet or mocknet network
14
14
15
15
``` typescript
16
16
import { StacksMainnet , StacksTestnet , StacksMocknet } from ' @stacks/network' ;
@@ -22,19 +22,19 @@ const testnet = new StacksTestnet();
22
22
const mocknet = new StacksMocknet ();
23
23
```
24
24
25
- Setting a custom node URL
25
+ ### Set a custom node URL
26
26
27
27
``` typescript
28
28
const network = new StacksMainnet ({ url: ' https://www.mystacksnode.com/' });
29
29
```
30
30
31
- Check if network is mainnet
31
+ ### Check if network is mainnet
32
32
33
33
``` typescript
34
34
const isMainnet = network .isMainnet ();
35
35
```
36
36
37
- Example usage in transaction builder
37
+ ### Network usage in transaction building
38
38
39
39
``` typescript
40
40
import { makeSTXTokenTransfer } from ' @stacks/transactions' ;
@@ -49,7 +49,29 @@ const txOptions = {
49
49
const transaction = await makeSTXTokenTransfer (txOptions );
50
50
```
51
51
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
53
75
54
76
``` typescript
55
77
const txBroadcastUrl = network .getBroadcastApiUrl ();
0 commit comments