@@ -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,58 @@ 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
+ import { createApiKeyMiddleware , createFetchFn , StacksMainnet } from ' @stacks/network' ;
58
+ import { broadcastResponse , getNonce , makeSTXTokenTransfer } from ' @stacks/transactions' ;
59
+
60
+ const myApiMiddleware = createApiKeyMiddleware (' example_e8e044a3_41d8b0fe_3dd3988ef302' );
61
+ const myFetchFn = createFetchFn (apiMiddleware ); // middlewares can be used to create a new fetch function
62
+ const myMainnet = new StacksMainnet ({ fetchFn: myFetchFn }); // the fetchFn options can be passed to a StacksNetwork to override the default fetch function
63
+
64
+ const txOptions = {
65
+ recipient: ' SP3FGQ8Z7JY9BWYZ5WM53E0M9NK7WHJF0691NZ159' ,
66
+ amount: 12345n ,
67
+ senderKey: ' b244296d5907de9864c0b0d51f98a13c52890be0404e83f273144cd5b9960eed01' ,
68
+ memo: ' some memo' ,
69
+ anchorMode: AnchorMode .Any ,
70
+ network: myMainnet , // make sure to pass in the custom network object
71
+ };
72
+ const transaction = await makeSTXTokenTransfer (txOptions ); // fee-estimation will use the custom fetchFn
73
+
74
+ const response = await broadcastResponse (transaction , myMainnet ); // make sure to broadcast via the custom network object
75
+
76
+ // stacks.js functions, which take a StacksNetwork object will use the custom fetchFn
77
+ const nonce = await getNonce (' SP3FGQ8Z7JY9BWYZ5WM53E0M9NK7WHJF0691NZ159' , myMainnet );
78
+ ```
79
+
80
+ ### Use custom middleware
81
+
82
+ Middleware can be used to hook into network calls before sending a request or after receiving a response.
83
+
84
+ ``` typescript
85
+ import { createFetchFn , RequestContext , ResponseContext , StacksTestnet } from ' @stacks/network' ;
86
+ import { broadcastResponse , getNonce , makeSTXTokenTransfer } from ' @stacks/transactions' ;
87
+
88
+ const preMiddleware = (ctx : RequestContext ) => {
89
+ ctx .init .headers = new Headers ();
90
+ ctx .init .headers .set (' x-foo' , ' bar' ); // override headers and set new `x-foo` header
91
+ };
92
+ const postMiddleware = (ctx : ResponseContext ) => {
93
+ console .log (await ctx .response .json ()); // log response body as json
94
+ };
95
+
96
+ const fetchFn = createFetchFn ({ pre: preMiddleware , post: preMiddleware }); // a middleware can contain `pre`, `post`, or both
97
+ const network = new StacksTestnet ({ fetchFn });
98
+
99
+ // stacks.js functions, which take a StacksNetwork object will use the custom fetchFn
100
+ const nonce = await getNonce (' SP3FGQ8Z7JY9BWYZ5WM53E0M9NK7WHJF0691NZ159' , network );
101
+ ```
102
+
103
+ ### Get various API URLs
53
104
54
105
``` typescript
55
106
const txBroadcastUrl = network .getBroadcastApiUrl ();
0 commit comments