mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-08 12:28:54 +00:00
Merge pull request #523 from Infisical/revise-node-sdk-docs
Revise docs for Node SDK
This commit is contained in:
@@ -64,7 +64,9 @@ These examples demonstrate how to store and fetch environment variables from [In
|
|||||||
### Initialize the Infisical client
|
### Initialize the Infisical client
|
||||||
|
|
||||||
```js
|
```js
|
||||||
await infisical.connect({
|
import InfisicalClient from "infisical-node";
|
||||||
|
|
||||||
|
const client = new InfisicalClient({
|
||||||
token: "your_infisical_token",
|
token: "your_infisical_token",
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
@@ -72,31 +74,31 @@ These examples demonstrate how to store and fetch environment variables from [In
|
|||||||
### Get a value
|
### Get a value
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const value = infisical.get("SOME_KEY");
|
const value = await client.getSecret("SOME_KEY");
|
||||||
```
|
```
|
||||||
|
|
||||||
### Example with Express
|
### Example with Express
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const express = require("express");
|
import InfisicalClient from "infisical-node";
|
||||||
const port = 3000;
|
import express from "express";
|
||||||
const infisical = require("infisical-node");
|
const app = express();
|
||||||
|
const PORT = 3000;
|
||||||
|
|
||||||
const main = async () => {
|
const client = InfisicalClient({
|
||||||
await infisical.connect({
|
token: "st.xxx.xxx",
|
||||||
token: "st.xxx.xxx",
|
});
|
||||||
});
|
|
||||||
|
|
||||||
// your application logic
|
// your application logic
|
||||||
|
|
||||||
app.get("/", (req, res) => {
|
app.get("/", async (req, res) => {
|
||||||
res.send(`Howdy, ${infisical.get("NAME")}!`);
|
const name = await client.getSecret("NAME");
|
||||||
});
|
res.send(`Hello! My name is: ${name.secretValue}`);
|
||||||
|
});
|
||||||
|
|
||||||
app.listen(port, async () => {
|
app.listen(PORT, async () => {
|
||||||
console.log(`App listening on port ${port}`);
|
console.log(`App listening on port ${port}`);
|
||||||
});
|
});
|
||||||
};
|
|
||||||
```
|
```
|
||||||
|
|
||||||
<Warning>
|
<Warning>
|
||||||
|
|||||||
+139
-106
@@ -2,7 +2,7 @@
|
|||||||
title: "Node"
|
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.
|
If you're working with Node.js, the official [infisical-node](https://github.com/Infisical/infisical-node) package is the easiest way to fetch and work with secrets for your application.
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
@@ -12,141 +12,174 @@ Run `npm` to add `infisical-node` to your project.
|
|||||||
npm install infisical-node --save
|
npm install infisical-node --save
|
||||||
```
|
```
|
||||||
|
|
||||||
## Initialization
|
## Configuration
|
||||||
|
|
||||||
Set up the Infisical client asynchronously as early as possible in your application by importing and initializing the global instance with `infisical.connect(options)`.
|
Import the SDK and create a client instance with your Infisical token.
|
||||||
|
|
||||||
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>
|
<Tabs>
|
||||||
<Tab title="ES6">
|
<Tab title="ES6">
|
||||||
```js
|
```js
|
||||||
import infisical from "infisical-node";
|
import InfisicalClient from "infisical-node";
|
||||||
|
|
||||||
|
const client = new InfisicalClient({
|
||||||
|
token: "your_infisical_token"
|
||||||
|
});
|
||||||
|
|
||||||
const main = async () => {
|
// your app logic
|
||||||
await infisical.connect({
|
|
||||||
token: "your_infisical_token",
|
|
||||||
});
|
|
||||||
|
|
||||||
// your app logic
|
|
||||||
}
|
|
||||||
|
|
||||||
main();
|
|
||||||
```
|
```
|
||||||
|
|
||||||
</Tab>
|
</Tab>
|
||||||
<Tab title="ES5">
|
<Tab title="ES5">
|
||||||
```js
|
```js
|
||||||
const infisical = require("infisical-node");
|
const InfisicalClient = require("infisical-node");
|
||||||
|
|
||||||
infisical.connect({
|
const client = new InfisicalClient({
|
||||||
token: "your_infisical_token"
|
token: "your_infisical_token"
|
||||||
})
|
});
|
||||||
.then(() => {
|
|
||||||
// your application logic
|
// your app logic
|
||||||
})
|
|
||||||
.catch(err => {
|
|
||||||
console.error('Error: ', err);
|
|
||||||
})
|
|
||||||
````
|
````
|
||||||
</Tab>
|
</Tab>
|
||||||
|
|
||||||
</Tabs>
|
</Tabs>
|
||||||
|
|
||||||
## Usage
|
<ResponseField name="options" type="object">
|
||||||
|
<Expandable title="properties">
|
||||||
To get the value of a secret, use `infisical.get(key)`.
|
<ResponseField name="token" type="string">
|
||||||
|
An [Infisical Token](/getting-started/dashboard/token) scoped to a project
|
||||||
### infisical.get(key)
|
and environment
|
||||||
|
</ResponseField>
|
||||||
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
|
<ResponseField
|
||||||
initialization step or if a value for the secret is not found in the fetched secrets.
|
name="siteURL"
|
||||||
|
type="string"
|
||||||
<ResponseField name="key" type="string" required>
|
default="https://app.infisical.com"
|
||||||
The key of the secret
|
>
|
||||||
|
Your self-hosted absolute site URL including the protocol (e.g.
|
||||||
|
`https://app.infisical.com`)
|
||||||
|
</ResponseField>
|
||||||
|
<ResponseField name="cacheTTL" type="number" default="300">
|
||||||
|
Time-to-live (in seconds) for refreshing cached secrets. Default: `300`.
|
||||||
|
</ResponseField>
|
||||||
|
<ResponseField name="debug" type="boolean" default="false">
|
||||||
|
Whether or not debug mode is on
|
||||||
|
</ResponseField>
|
||||||
|
</Expandable>
|
||||||
</ResponseField>
|
</ResponseField>
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
### infisical.getSecret(secretName, options)
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const value = infisical.get("SOME_KEY");
|
const secret = await infisical.getSecret("API_KEY");
|
||||||
|
const value = secret.secretValue; // get its value
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Retrieve a secret from Infisical.
|
||||||
|
|
||||||
|
By default, `getSecret()` returns a personal secret. If not found, it returns a shared secret, or tries to retrieve the value from `process.env`.
|
||||||
|
|
||||||
|
|
||||||
|
<ResponseField name="secretName" type="string" required>
|
||||||
|
The key of the secret to retrieve
|
||||||
|
</ResponseField>
|
||||||
|
<ResponseField name="options" type="object">
|
||||||
|
<Expandable title="properties">
|
||||||
|
<ResponseField name="type" type="string">
|
||||||
|
"personal" (default) or "shared".
|
||||||
|
</ResponseField>
|
||||||
|
</Expandable>
|
||||||
|
</ResponseField>
|
||||||
|
|
||||||
|
### infisical.createSecret(secretName, secretValue, options)
|
||||||
|
|
||||||
|
```js
|
||||||
|
const newApiKey = await infisical.createSecret("API_KEY", "FOO");
|
||||||
|
```
|
||||||
|
|
||||||
|
Create a new secret in Infisical.
|
||||||
|
|
||||||
|
<ResponseField name="secretName" type="string" required>
|
||||||
|
The key of the secret to create
|
||||||
|
</ResponseField>
|
||||||
|
<ResponseField name="secretName" type="string" required>
|
||||||
|
The value of the secret to create
|
||||||
|
</ResponseField>
|
||||||
|
<ResponseField name="options" type="object">
|
||||||
|
<Expandable title="properties">
|
||||||
|
<ResponseField name="type" type="string">
|
||||||
|
"shared" (default) or "personal". A personal secret can only be created if a shared secret with the same name exists.
|
||||||
|
</ResponseField>
|
||||||
|
</Expandable>
|
||||||
|
</ResponseField>
|
||||||
|
|
||||||
|
### infisical.updateSecret(secretName, secretValue, options)
|
||||||
|
|
||||||
|
```js
|
||||||
|
const updatedApiKey = await infisical.updateSecret("API_KEY", "BAR");
|
||||||
|
```
|
||||||
|
|
||||||
|
Update an existing secret in Infisical.
|
||||||
|
|
||||||
|
<ResponseField name="secretName" type="string" required>
|
||||||
|
The key of the secret to update
|
||||||
|
</ResponseField>
|
||||||
|
<ResponseField name="secretName" type="string" required>
|
||||||
|
The new value of the secret
|
||||||
|
</ResponseField>
|
||||||
|
<ResponseField name="options" type="object">
|
||||||
|
<Expandable title="properties">
|
||||||
|
<ResponseField name="type" type="string">
|
||||||
|
"shared" (default) or "personal".
|
||||||
|
</ResponseField>
|
||||||
|
</Expandable>
|
||||||
|
</ResponseField>
|
||||||
|
|
||||||
|
### infisical.deleteSecret(secretName, options)
|
||||||
|
|
||||||
|
```js
|
||||||
|
const deletedSecret = await infisical.deleteSecret("API_KEY");
|
||||||
|
```
|
||||||
|
|
||||||
|
Delete a secret in Infisical.
|
||||||
|
|
||||||
|
<ResponseField name="secretName" type="string" required>
|
||||||
|
The key of the secret to delete
|
||||||
|
</ResponseField>
|
||||||
|
<ResponseField name="options" type="object">
|
||||||
|
<Expandable title="properties">
|
||||||
|
<ResponseField name="type" type="string">
|
||||||
|
"shared" (default) or "personal". Note that deleting a shared secret also deletes all associated personal secrets.
|
||||||
|
</ResponseField>
|
||||||
|
</Expandable>
|
||||||
|
</ResponseField>
|
||||||
|
|
||||||
## Example with Express
|
## Example with Express
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const express = require("express");
|
import InfisicalClient from "infisical-node";
|
||||||
const port = 3000;
|
import express from "express";
|
||||||
const infisical = require("infisical-node");
|
const app = express();
|
||||||
|
const PORT = 3000;
|
||||||
|
|
||||||
const main = async () => {
|
const client = new InfisicalClient({
|
||||||
await infisical.connect({
|
token: "YOUR_INFISICAL_TOKEN"
|
||||||
token: "st.xxx.xxx",
|
});
|
||||||
});
|
|
||||||
|
|
||||||
// your application logic
|
app.get("/", async (req, res) => {
|
||||||
|
// access value
|
||||||
|
const name = await client.getSecret("NAME");
|
||||||
|
res.send(`Hello! My name is: ${name.secretValue}`);
|
||||||
|
});
|
||||||
|
|
||||||
app.get("/", (req, res) => {
|
app.listen(PORT, async () => {
|
||||||
res.send(`Howdy, ${infisical.get("NAME")}!`);
|
// initialize client
|
||||||
});
|
console.log(`App listening on port ${port}`);
|
||||||
|
});
|
||||||
app.listen(port, async () => {
|
|
||||||
console.log(`App listening on port ${port}`);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
This example demonstrates how to use the Infisical SDK with an Express application. The application retrieves a secret named "NAME" and responds to requests with a greeting that includes the secret value.
|
||||||
|
|
||||||
<Warning>
|
<Warning>
|
||||||
We do not recommend hardcoding your [Infisical
|
We do not recommend hardcoding your [Infisical
|
||||||
Token](/getting-started/dashboard/token). Setting it as an environment
|
Token](/getting-started/dashboard/token). Setting it as an environment
|
||||||
|
|||||||
Reference in New Issue
Block a user