Files
infisical/docs/sdks/languages/node.mdx
T
2023-04-23 15:51:42 +03:00

188 lines
5.0 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 and work with secrets for your application.
## Installation
Run `npm` to add `infisical-node` to your project.
```bash
npm install infisical-node --save
```
## Configuration
Import the SDK and create a client instance with your Infisical token.
<Tabs>
<Tab title="ES6">
```js
import InfisicalClient from "infisical-node";
const client = new InfisicalClient({
token: "your_infisical_token"
});
// your app logic
```
</Tab>
<Tab title="ES5">
```js
const InfisicalClient = require("infisical-node");
const client = new InfisicalClient({
token: "your_infisical_token"
});
// your app logic
````
</Tab>
</Tabs>
<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="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>
## Usage
### infisical.getSecret(secretName, options)
```js
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
```js
import InfisicalClient from "infisical-node";
import express from "express";
const app = express();
const PORT = 3000;
const client = new InfisicalClient({
token: "YOUR_INFISICAL_TOKEN"
});
app.get("/", async (req, res) => {
// access value
const name = await client.getSecret("NAME");
res.send(`Hello! My name is: ${name.secretValue}`);
});
app.listen(PORT, async () => {
// initialize client
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>
We do not recommend hardcoding your [Infisical
Token](/getting-started/dashboard/token). Setting it as an environment
variable would be best.
</Warning>