Updated Node.js docs

This commit is contained in:
Daniel Hougaard
2023-12-23 15:10:30 +04:00
parent a3739cfe50
commit 40d16fa996

View File

@ -9,39 +9,52 @@ If you're working with Node.js, the official [infisical-node](https://github.com
```js
import express from "express";
import InfisicalClient from "infisical-node";
import { InfisicalClient, LogLevel } from "@infisical/sdk";
const app = express();
const PORT = 3000;
const client = new InfisicalClient({
token: "YOUR_INFISICAL_TOKEN"
clientId: "YOUR_CLIENT_ID",
clientSecret: "YOUR_CLIENT_SECRET",
logLevel: LogLevel.Error
});
app.get("/", async (req, res) => {
// access value
const name = await client.getSecret("NAME");
res.send(`Hello! My name is: ${name.secretValue}`);
// access value
const name = await client.getSecret({
environment: "dev",
projectId: "656dba7f979ebd6652586669",
path: "/",
type: "shared",
secretName: "NAME"
});
res.send(`Hello! My name is: ${name.secretValue}`);
});
app.listen(PORT, async () => {
console.log(`App listening on port ${PORT}`);
// initialize client
console.log(`App listening on port ${port}`);
});
```
This example demonstrates how to use the Infisical Node 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>
We do not recommend hardcoding your [Infisical
Token](/documentation/platform/token). Setting it as an environment
variable would be best.
We do not recommend hardcoding your [Machine Identity Tokens](/platform/identities/overview). Setting it as an environment variable would be best.
</Warning>
## Installation
Run `npm` to add `infisical-node` to your project.
Run `npm` to add `@infisical/sdk` to your project.
```console
$ npm install infisical-node --save
$ npm install @infisical/sdk
```
## Configuration
@ -51,22 +64,27 @@ Import the SDK and create a client instance with your [Infisical Token](/documen
<Tabs>
<Tab title="ES6">
```js
import InfisicalClient from "infisical-node";
import { InfisicalClient, LogLevel } from "@infisical/sdk";
const client = new InfisicalClient({
token: "your_infisical_token"
clientId: "YOUR_CLIENT_ID",
clientSecret: "YOUR_CLIENT_SECRET",
logLevel: LogLevel.Error
});
```
</Tab>
<Tab title="ES5">
```js
const InfisicalClient = require("infisical-node");
const { InfisicalClient, LogLevel } = require("@infisical/sdk");
const client = new InfisicalClient({
token: "your_infisical_token"
clientId: "YOUR_CLIENT_ID",
clientSecret: "YOUR_CLIENT_SECRET",
logLevel: LogLevel.Error
});
````
</Tab>
</Tabs>
@ -74,134 +92,212 @@ Import the SDK and create a client instance with your [Infisical Token](/documen
### Parameters
<ParamField query="options" type="object">
<Expandable title="properties">
<ParamField query="token" type="string" optional>
An [Infisical Token](/documentation/platform/token) scoped to a project
and environment
</ParamField>
<ParamField
query="siteURL"
type="string"
default="https://app.infisical.com"
optional
>
Your self-hosted absolute site URL including the protocol (e.g.
`https://app.infisical.com`)
</ParamField>
<ParamField query="cacheTTL" type="number" default="300" optional>
Time-to-live (in seconds) for refreshing cached secrets. Default: `300`.
</ParamField>
<ParamField query="debug" type="boolean" default="false" optional>
Whether or not debug mode is on
</ParamField>
</Expandable>
<Expandable title="properties">
<ParamField query="clientId" type="string" optional>
Your machine identity client ID.
</ParamField>
<ParamField query="clientSecret" type="string" optional>
Your machine identity client secret.
</ParamField>
<ParamField query="accessToken" type="string" optional>
An access token obtained from the machine identity login endpoint.
</ParamField>
<ParamField query="siteUrl" type="string" default="https://app.infisical.com" optional>
Your self-hosted absolute site URL including the protocol (e.g. `https://app.infisical.com`)
</ParamField>
<ParamField query="logLevel" type="enum" default="Error" optional>
The level of logs you wish to log The logs are derived from Rust, as we have written our base SDK in Rust.
</ParamField>
</Expandable>
</ParamField>
## Caching
The SDK caches every secret and updates it periodically based on the provided `cacheTTL`. For example, if `cacheTTL` of `300` is provided, then a secret will be refetched 5 minutes after the first fetch; if the fetch fails, the cached secret is returned.
<Tip>
For optimal performance, we recommend creating a single instance of the Infisical client and exporting it to be used across your entire app to take advantage of caching benefits.
</Tip>
## Working with Secrets
### client.getAllSecrets()
### client.listSecrets(options)
```js
const secrets = await client.getAllSecrets();
const secrets = await client.listSecrets({
environment: "dev",
projectId: "PROJECT_ID",
path: "/foo/bar/",
includeImports: false
});
```
Retrieve all secrets within the Infisical project and environment that client is connected to
### client.getSecret(secretName, options)
### Parameters
<ParamField query="Parameters" type="object">
<Expandable title="properties">
<ParamField query="environment" type="string">
The slug name (dev, prod, etc) of the environment from where secrets should be fetched from.
</ParamField>
<ParamField query="projectId" type="string">
The project ID where the secret lives in.
</ParamField>
<ParamField query="path" type="string" optional>
The path from where secrets should be fetched from.
</ParamField>
<ParamField query="includeImports" type="string" default="https://app.infisical.com" optional>
Whether or not to include imported secrets from the current path. Read about [secret import](/platform/secret-reference)
</ParamField>
</Expandable>
</ParamField>
### client.getSecret(options)
```js
const secret = await client.getSecret("API_KEY");
const value = secret.secretValue; // get its value
const secret = await client.getSecret({
environment: "dev",
projectId: "PROJECT_ID",
secretName: "API_KEY",
path: "/",
type: "shared"
});
```
Retrieve a secret from Infisical.
By default, `getSecret()` fetches and returns a personal secret. If not found, it returns a shared secret, or tries to retrieve the value from `process.env`. If a secret is fetched, `getSecret()` caches it to reduce excessive calls and re-fetches periodically based on the `cacheTTL` option (default is `300` seconds) when initializing the client — for more information, see the caching section.
By default, `getSecret()` fetches and returns a shared secret.
### Parameters
<ParamField query="secretName" type="string" required>
The key of the secret to retrieve
</ParamField>
<ParamField query="options" type="object" optional>
<Expandable title="properties">
<ParamField query="type" type="string" default="personal" optional>
The type of the secret. Valid options are "shared" or "personal"
</ParamField>
</Expandable>
<ParamField query="Parameters" type="object" optional>
<Expandable title="properties">
<ParamField query="secretName" type="string">
The key of the secret to retrieve.
</ParamField>
<ParamField query="projectId" type="string">
The project ID where the secret lives in.
</ParamField>
<ParamField query="environment" type="string">
The slug name (dev, prod, etc) of the environment from where secrets should be fetched from.
</ParamField>
<ParamField query="path" type="string" optional>
The path from where secret should be fetched from.
</ParamField>
<ParamField query="type" type="string" optional>
The type of the secret. Valid options are "shared" or "personal". If not specified, the default value is "shared".
</ParamField>
</Expandable>
</ParamField>
### client.createSecret(secretName, secretValue, options)
### client.createSecret(options)
```js
const newApiKey = await client.createSecret("API_KEY", "FOO");
const newApiKey = await client.createSecret({
projectId: "PROJECT_ID",
environment: "dev",
secretName: "API_KEY",
secretValue: "SECRET VALUE",
path: "/",
type: "shared"
});
```
Create a new secret in Infisical.
<ParamField query="secretName" type="string" required>
The key of the secret to create
</ParamField>
<ParamField query="secretName" type="string" required>
The value of the secret to create
</ParamField>
<ParamField query="options" type="object" default="object" optional>
<Expandable title="properties">
<ParamField query="type" type="string" default="shared" optional>
The type of the secret. Valid options are "shared" or "personal". A personal secret can only be created if a shared secret with the same name exists.
</ParamField>
</Expandable>
<ParamField query="Parameters" type="object" optional>
<Expandable title="properties">
<ParamField query="secretName" type="string">
The key of the secret to create.
</ParamField>
<ParamField query="secretValue" type="string">
The value of the secret.
</ParamField>
<ParamField query="projectId" type="string">
The project ID where the secret lives in.
</ParamField>
<ParamField query="environment" type="string">
The slug name (dev, prod, etc) of the environment from where secrets should be fetched from.
</ParamField>
<ParamField query="path" type="string" optional>
The path from where secret should be created.
</ParamField>
<ParamField query="type" type="string" optional>
The type of the secret. Valid options are "shared" or "personal". If not specified, the default value is "shared".
</ParamField>
</Expandable>
</ParamField>
### client.updateSecret(secretName, secretValue, options)
### client.updateSecret(options)
```js
const updatedApiKey = await client.updateSecret("API_KEY", "BAR");
const updatedApiKey = await client.updateSecret({
secretName: "API_KEY",
secretValue: "NEW SECRET VALUE",
projectId: "PROJECT_ID",
environment: "dev",
path: "/",
type: "shared"
});
```
Update an existing secret in Infisical.
### Parameters
<ParamField query="secretName" type="string" required>
The key of the secret to update
</ParamField>
<ParamField query="secretName" type="string" required>
The new value of the secret
</ParamField>
<ParamField query="options" type="object" default="object" optional>
<Expandable title="properties">
<ParamField query="type" type="string" default="shared" optional>
The type of the secret. Valid options are "shared" or "personal"
</ParamField>
</Expandable>
<ParamField query="Parameters" type="object" optional>
<Expandable title="properties">
<ParamField query="secretName" type="string">
The key of the secret to update.
</ParamField>
<ParamField query="secretValue" type="string">
The new value of the secret.
</ParamField>
<ParamField query="projectId" type="string">
The project ID where the secret lives in.
</ParamField>
<ParamField query="environment" type="string">
The slug name (dev, prod, etc) of the environment from where secrets should be fetched from.
</ParamField>
<ParamField query="path" type="string" optional>
The path from where secret should be updated.
</ParamField>
<ParamField query="type" type="string" optional>
The type of the secret. Valid options are "shared" or "personal". If not specified, the default value is "shared".
</ParamField>
</Expandable>
</ParamField>
### client.deleteSecret(secretName, options)
### client.deleteSecret(options)
```js
const deletedSecret = await client.deleteSecret("API_KEY");
const deletedSecret = await client.deleteSecret({
secretName: "API_KEY",
environment: "dev",
projectId: "PROJECT_ID",
path: "/",
type: "shared"
});
```
Delete a secret in Infisical.
<ParamField query="secretName" type="string" required>
The key of the secret to delete
<ParamField query="Parameters" type="object" optional>
<Expandable title="properties">
<ParamField query="secretName" type="string">
The key of the secret to update.
</ParamField>
<ParamField query="projectId" type="string">
The project ID where the secret lives in.
</ParamField>
<ParamField query="environment" type="string">
The slug name (dev, prod, etc) of the environment from where secrets should be fetched from.
</ParamField>
<ParamField query="path" type="string" optional>
The path from where secret should be deleted.
</ParamField>
<ParamField query="type" type="string" optional>
The type of the secret. Valid options are "shared" or "personal". If not specified, the default value is "shared".
</ParamField>
</Expandable>
</ParamField>
<ParamField query="options" type="object" default="object" optional>
<Expandable title="properties">
<ParamField query="type" type="string" default="shared" optional>
The type of the secret. Valid options are "shared" or "personal". Note that deleting a shared secret also deletes all associated personal secrets.
</ParamField>
</Expandable>
</ParamField>