You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
*`pkgJson`: The content of the `package.json` you want to create a `User-Agent` string for
67
+
68
+
## Advanced
69
+
70
+
### Specifying custom user agent
71
+
72
+
The `SocketSdk` constructor accepts an `options` object as its second argument and there a `userAgent` key with a string value can be specified. If specified then that user agent will be prepended to the SDK user agent. See this example:
73
+
74
+
```js
75
+
constclient=newSocketSdk('yourApiKeyHere', {
76
+
userAgent:'example/1.2.3 (http://example.com/)'
77
+
})
78
+
```
79
+
80
+
Which results in the [HTTP `User-Agent` header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent):
To easily create a user agent for your code you can use the additional export `createUserAgentFromPkgJson()` like this, assuming `pkgJson` contains your parsed `package.json`:
87
+
88
+
```js
89
+
constclient=newSocketSdk('yourApiKeyHere', {
90
+
userAgent:createUserAgentFromPkgJson(pkgJson)
91
+
})
92
+
```
93
+
94
+
Specifying a custom user agent is good practice when shipping a piece of code that others can use to make requests. Eg. [our CLI](https://github.com/SocketDev/socket-cli-js) uses this option to identify requests coming from it + mentioning which version of it that is used.
95
+
63
96
## See also
64
97
65
98
*[Socket API Reference](https://docs.socket.dev/reference)
0 commit comments