mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-09-22 13:39:35 +00:00
155 lines
4.2 KiB
Plaintext
155 lines
4.2 KiB
Plaintext
---
|
|
title: "Node"
|
|
---
|
|
|
|
If you're working with Node.js, the official [infisical-node](https://github.com/Infisical/infisical-node) package is the easiest way to fetch secrets for your application.
|
|
|
|
## Installation
|
|
|
|
Run `npm` to add `infisical-node` to your project.
|
|
|
|
```bash
|
|
npm install infisical-node --save
|
|
```
|
|
|
|
## Initialization
|
|
|
|
Set up the Infisical client asynchronously as early as possible in your application by importing and initializing the global instance with `infisical.connect(options)`.
|
|
|
|
This methods fetches back all the secrets in the project and environment accessible by the token passed in `options`.
|
|
|
|
### infisical.connect(options)
|
|
|
|
Updates the global instance of the Infisical client with a connection to an Infisical project and fetches back secrets if supplied with an [Infisical Token](/getting-started/dashboard/token).
|
|
|
|
<ResponseField name="options" type="object">
|
|
<Expandable title="properties">
|
|
<ResponseField name="token" type="string">
|
|
An [Infisical Token](/getting-started/dashboard/token) scoped to a project
|
|
and environment
|
|
</ResponseField>
|
|
<ResponseField
|
|
name="siteURL"
|
|
type="string"
|
|
default="https://app.infisical.com"
|
|
>
|
|
Your self-hosted absolute site URL including the protocol (e.g.
|
|
`https://app.infisical.com`)
|
|
</ResponseField>
|
|
<ResponseField name="debug" type="boolean" default="false">
|
|
Whether or not debug mode is on
|
|
</ResponseField>
|
|
<ResponseField name="attachToProcessEnv" type="boolean" default="false">
|
|
Whether or not to attach fetched secrets to `process.env`
|
|
</ResponseField>
|
|
</Expandable>
|
|
</ResponseField>
|
|
|
|
### infisical.createConnection(options)
|
|
|
|
Returns a local instance of the Infisical client with a connection to an Infisical project and fetches back secrets if supplied with an [Infisical Token](/getting-started/dashboard/token).
|
|
|
|
This method is useful if you wish to connect to two or more Infisical projects within your app.
|
|
|
|
<ResponseField name="options" type="object">
|
|
<Expandable title="properties">
|
|
<ResponseField name="token" type="string">
|
|
An [Infisical Token](/getting-started/dashboard/token) scoped to a project
|
|
and environment
|
|
</ResponseField>
|
|
<ResponseField
|
|
name="siteURL"
|
|
type="string"
|
|
default="https://app.infisical.com"
|
|
>
|
|
Your self-hosted absolute site URL including the protocol (e.g.
|
|
`https://app.infisical.com`)
|
|
</ResponseField>
|
|
<ResponseField name="debug" type="boolean" default="false">
|
|
Whether or not debug mode is on
|
|
</ResponseField>
|
|
</Expandable>
|
|
</ResponseField>
|
|
|
|
<Tabs>
|
|
<Tab title="ES6">
|
|
```js
|
|
import infisical from "infisical-node";
|
|
|
|
const main = async () => {
|
|
await infisical.connect({
|
|
token: "your_infisical_token",
|
|
});
|
|
|
|
// your app logic
|
|
}
|
|
|
|
main();
|
|
```
|
|
|
|
</Tab>
|
|
<Tab title="ES5">
|
|
```js
|
|
const infisical = require("infisical-node");
|
|
|
|
infisical.connect({
|
|
token: "your_infisical_token"
|
|
})
|
|
.then(() => {
|
|
// your application logic
|
|
})
|
|
.catch(err => {
|
|
console.error('Error: ', err);
|
|
})
|
|
````
|
|
</Tab>
|
|
|
|
</Tabs>
|
|
|
|
## Usage
|
|
|
|
To get the value of a secret, use `infisical.get(key)`.
|
|
|
|
### infisical.get(key)
|
|
|
|
Return the value of the secret with the specified `key`. Note that the Infisical client falls back to `process.env` if `token` is `undefined` during the
|
|
initialization step or if a value for the secret is not found in the fetched secrets.
|
|
|
|
<ResponseField name="key" type="string" required>
|
|
The key of the secret
|
|
</ResponseField>
|
|
|
|
```js
|
|
const value = infisical.get("SOME_KEY");
|
|
```
|
|
|
|
## Example with Express
|
|
|
|
```js
|
|
const express = require("express");
|
|
const port = 3000;
|
|
const infisical = require("infisical-node");
|
|
|
|
const main = async () => {
|
|
await infisical.connect({
|
|
token: "st.xxx.xxx",
|
|
});
|
|
|
|
// your application logic
|
|
|
|
app.get("/", (req, res) => {
|
|
res.send(`Howdy, ${infisical.get("NAME")}!`);
|
|
});
|
|
|
|
app.listen(port, async () => {
|
|
console.log(`App listening on port ${port}`);
|
|
});
|
|
};
|
|
```
|
|
|
|
<Warning>
|
|
We do not recommend hardcoding your [Infisical
|
|
Token](/getting-started/dashboard/token). Setting it as an environment
|
|
variable would be best.
|
|
</Warning>
|