Node.js

Encrypting/Decrypting data with our backend SDKs may expose you to greater compliance burden because your server needs to handle plaintext data. Instead, we recommend using Relay or our Client-Side SDKs to encrypt data.

Getting started


Install the SDK


First, let's install the Evervault SDK using your package manager of choice.

Initialize the SDK


Now, let's initialize the SDK using our App's ID and our App's API key. If you don't have an API key yet, you can get one by creating an App in the Evervault Dashboard.

Relay proxies


SDKs that support using proxies can be configured to use Relay, use the createRelayHttpsAgent() function to return a pre-configured HTTPSProxyAgent. The Agent will use the HTTP CONNECT protocol to establish a connection to Relay. Below is how you would configure Relay with the Stripe Node.js SDK.

The HTTPSProxyAgent can also be used with popular HTTP clients like axios. Its important to note that the URL of the request is not the Relay URL but the URL of the target.

Reference


Evervault


The SDK constructor accepts two parameters; your App's ID and your App's API key.

Parameters

  • appIdRequiredString

    Your Evervault App's ID.

  • apiKeyRequiredString

    Your Evervault App's API Key.

encrypt()


Encrypts data using Evervault Encryption.

To encrypt strings using the Node.js SDK, simply pass a String or an Object into the evervault.encrypt() function. To encrypt a file, pass a Buffer.

The encrypted data can be stored in your database or file storage as normal. Evervault Strings can be used across all of our products. Evervault File Encryption is currently in Beta, and files can only be decrypted with Relay.

Parameters

  • dataRequiredString | Boolean | Number | Object | Buffer

    The data to encrypt.

decrypt()


Decrypts data previously encrypted with the encrypt() function or through Relay. An API key with the decrypt permission must be used to perform this operation.

Parameters

  • dataRequiredString | Object | Buffer

    The data to decrypt.

PCI Compliance

Decrypting data with our backend SDKs is not available if you are part of the PCI or HIPAA compliance use cases. Instead you can:

  • Use Relay to decrypt data before it reaches third-party services.
  • Use Functions or Enclaves to process encrypted data.

createClientSideDecryptToken()


Client Side Decrypt Tokens are versatile and short-lived tokens that frontend applications can utilise to decrypt data previously encrypted through Evervault. Client Side Decrypt Tokens are restricted to specific payloads.

By default, a Client Side Decrypt Token will live for 5 minutes into the future. The maximum time to live of the token is 10 minutes into the future.

Parameters

  • payloadRequiredString | Object | Buffer

    The payload containing the encrypted data that the token will be used to decrypt.

  • timeToLiveNumber

    The time the token will expire. Defaults to 5 minutes in the future.

run()


Lets you invoke an Evervault Function with a given payload.

Parameters

  • functionNameRequiredString

    The name of the function to invoke.

  • dataRequiredObject

    The payload to pass to the function.

Successful Function runs will return an object containing a Function Run ID and the result from your Function in the following format:

createRunToken()


Creates a single use, time bound token (5 minutes) for invoking an Evervault Function with a given payload. Run Tokens can be used to invoke an Evervault Function client-side without providing a sensitive API Key.

Parameters

  • functionNameRequiredString

    The name of the function to invoke.

  • dataObject

    The data you want to send to the function. If not provided, a run token will be created, and the payload will not be validated when the function is executed.

When you create a Run Token, the SDK will return a JSON object containing your token.

Run Tokens can then be used to authenticate Function runs from the client-side.

enableOutboundRelay()


We recommend using the createRelayHttpsAgent() function instead of this function if the SDK you are using supports proxy agents.

Automatically configures your application to proxy requests through Relay when making requests to domains you have configured Relays for inside of the Evervault Dashboard. See Relay to learn more.

Parameters

  • options.decryptionDomainsString[]

    Requests sent to any of the domains listed will be proxied through Relay. This will override the configuration created in the Evervault dashboard.

  • options.debugRequestsBoolean

    Output request domains and whether they were sent through Relay.

createRelayHttpsAgent()


The HTTPSProxyAgent allows for more granular control over what requests gets sent through Relay. This can be passed to any client that supports the Node.js Agent as a parameter.

createEnclaveHttpsAgent()


Returns a HTTPS Agent which can be used to attest all TLS connections to your Evervault Enclave. See Enclave's TLS Attestation to learn more.

Parameters

  • enclaveAttestationDataObject

    A mapping of Enclave names to their PCRs or Callbacks which resolve to PCRs. This is optional. When included, the connection will only be attested when the PCRs match exactly. The provided data can be either a single Object, or an Array of Objects to allow roll-over between different sets of PCRs. If not provided, the attestation doc and its signature will be validated but the PCRs will be ignored.

  • evervaultAttestationBindings

    The Evervault Attestation Bindings distributed as @evervault/attestation-bindings on npm. For testing, you can provide a mock object with a single async attestEnclave function.

Using static PCRs


The most simple way to set up your Evervault client to attest Enclaves is to provide hardcoded PCRs. This approach is not recommended for production deployments, as your clients will fall out of sync from the Enclave during deployments.

Using the Evervault API as a PCR Provider


The Evervault API exposes an endpoint to retrieve the PCRs for all active deployments of an Enclave. This can be used to keep your Client in sync with your Enclave across deployments.