diff --git a/docs/getting-started/quickstart.mdx b/docs/getting-started/quickstart.mdx index f19bd1120..7fca86d4d 100644 --- a/docs/getting-started/quickstart.mdx +++ b/docs/getting-started/quickstart.mdx @@ -64,7 +64,9 @@ These examples demonstrate how to store and fetch environment variables from [In ### Initialize the Infisical client ```js - await infisical.connect({ + import InfisicalClient from "infisical-node"; + + const client = new InfisicalClient({ token: "your_infisical_token", }); ``` @@ -72,31 +74,31 @@ These examples demonstrate how to store and fetch environment variables from [In ### Get a value ```js - const value = infisical.get("SOME_KEY"); + const value = await client.getSecret("SOME_KEY"); ``` ### Example with Express ```js - const express = require("express"); - const port = 3000; - const infisical = require("infisical-node"); + import InfisicalClient from "infisical-node"; + import express from "express"; + const app = express(); + const PORT = 3000; - const main = async () => { - await infisical.connect({ - token: "st.xxx.xxx", - }); + const client = InfisicalClient({ + token: "st.xxx.xxx", + }); - // your application logic + // your application logic - app.get("/", (req, res) => { - res.send(`Howdy, ${infisical.get("NAME")}!`); - }); + app.get("/", async (req, res) => { + const name = await client.getSecret("NAME"); + res.send(`Hello! My name is: ${name.secretValue}`); + }); - app.listen(port, async () => { - console.log(`App listening on port ${port}`); - }); - }; + app.listen(PORT, async () => { + console.log(`App listening on port ${port}`); + }); ``` diff --git a/docs/sdks/languages/node.mdx b/docs/sdks/languages/node.mdx index 58002392f..cb56f50d4 100644 --- a/docs/sdks/languages/node.mdx +++ b/docs/sdks/languages/node.mdx @@ -2,7 +2,7 @@ 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 @@ -12,141 +12,174 @@ Run `npm` to add `infisical-node` to your project. 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)`. - -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). - - - - - An [Infisical Token](/getting-started/dashboard/token) scoped to a project - and environment - - - Your self-hosted absolute site URL including the protocol (e.g. - `https://app.infisical.com`) - - - Whether or not debug mode is on - - - Whether or not to attach fetched secrets to `process.env` - - - - -### 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. - - - - - An [Infisical Token](/getting-started/dashboard/token) scoped to a project - and environment - - - Your self-hosted absolute site URL including the protocol (e.g. - `https://app.infisical.com`) - - - Whether or not debug mode is on - - - +Import the SDK and create a client instance with your Infisical token. ```js - import infisical from "infisical-node"; + import InfisicalClient from "infisical-node"; + + const client = new InfisicalClient({ + token: "your_infisical_token" + }); - const main = async () => { - await infisical.connect({ - token: "your_infisical_token", - }); - - // your app logic - } - - main(); + // your app logic ``` ```js - const infisical = require("infisical-node"); + const InfisicalClient = require("infisical-node"); - infisical.connect({ - token: "your_infisical_token" - }) - .then(() => { - // your application logic - }) - .catch(err => { - console.error('Error: ', err); - }) + const client = new InfisicalClient({ + token: "your_infisical_token" + }); + + // your app logic ```` -## 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. - - - The key of the secret + + + + An [Infisical Token](/getting-started/dashboard/token) scoped to a project + and environment + + + Your self-hosted absolute site URL including the protocol (e.g. + `https://app.infisical.com`) + + + Time-to-live (in seconds) for refreshing cached secrets. Default: `300`. + + + Whether or not debug mode is on + + +## Usage + +### infisical.getSecret(secretName, options) + ```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`. + + + + The key of the secret to retrieve + + + + + "personal" (default) or "shared". + + + + +### infisical.createSecret(secretName, secretValue, options) + +```js +const newApiKey = await infisical.createSecret("API_KEY", "FOO"); +``` + +Create a new secret in Infisical. + + + The key of the secret to create + + + The value of the secret to create + + + + + "shared" (default) or "personal". A personal secret can only be created if a shared secret with the same name exists. + + + + +### infisical.updateSecret(secretName, secretValue, options) + +```js +const updatedApiKey = await infisical.updateSecret("API_KEY", "BAR"); +``` + +Update an existing secret in Infisical. + + + The key of the secret to update + + + The new value of the secret + + + + + "shared" (default) or "personal". + + + + +### infisical.deleteSecret(secretName, options) + +```js +const deletedSecret = await infisical.deleteSecret("API_KEY"); +``` + +Delete a secret in Infisical. + + + The key of the secret to delete + + + + + "shared" (default) or "personal". Note that deleting a shared secret also deletes all associated personal secrets. + + + + ## Example with Express ```js -const express = require("express"); -const port = 3000; -const infisical = require("infisical-node"); +import InfisicalClient from "infisical-node"; +import express from "express"; +const app = express(); +const PORT = 3000; -const main = async () => { - await infisical.connect({ - token: "st.xxx.xxx", - }); +const client = new InfisicalClient({ + token: "YOUR_INFISICAL_TOKEN" +}); - // 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) => { - res.send(`Howdy, ${infisical.get("NAME")}!`); - }); - - app.listen(port, async () => { - console.log(`App listening on port ${port}`); - }); -}; +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. + We do not recommend hardcoding your [Infisical Token](/getting-started/dashboard/token). Setting it as an environment