Search.../

getSecretValue( )

Developer Preview

Retrieves the secret value specified by the secret name.

Description

The getSecretValue() function returns a Promise that resolves to the value of the secret with the specified given name.

Note: Only use a secret's value in the backend code. Returning the secret value in the frontend is a security risk.

Admin Method

This function requires elevated permissions to run. This function is not universal and runs only on the backend.

Syntax

function getSecretValue(name: string): Promise<GetSecretValueResponse>

getSecretValue Parameters

NAME
TYPE
DESCRIPTION
name
string

The name of the secret to get the value of.

Returns

Fulfilled - The value of the secret. Rejected - Error message.

Return Type:

Promise<
GetSecretValueResponse
>
NAME
TYPE
DESCRIPTION
value
string

The plaintext, unencrypted value of the secret.

Was this helpful?

Get a secret and use it to fetch a JSON from a 3rd-party service (dashboard page code)

Copy Code
1import { secrets } from 'wix-secrets-backend.v2';
2import {getJSON} from 'wix-fetch';
3
4export function getSomeJSON() {
5 return secrets.getSecretValue('myApiKeyName')
6 .then((secret) => {
7 return getJSON(`https://someapi.com/api/someendpoint?apiKey=${secret}`);
8 })
9 .catch((error) => {
10 console.error(error);
11 });
12}
13
14export function getFirstSecretValue() {
15 return secrets.listSecretInfo()
16 .then((secrets) => {
17 return secrets.getSecretValue(secrets[0].name);
18 })
19 .catch((error) => {
20 console.error(error);
21 });
22}
23
Get a secret and use it to fetch a JSON from a 3rd-party service (export from backend code)

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { secrets } from 'wix-secrets-backend.v2';
3import { getJSON } from 'wix-fetch';
4
5export const getSomeJSON = webMethod(Permissions.Anyone, () => {
6 return secrets.getSecretValue('myApiKeyName')
7 .then((secret) => {
8 return getJSON(`https://someapi.com/api/someendpoint?apiKey=${secret}`);
9 })
10 .catch((error) => {
11 console.error(error);
12 });
13});
14
15export const getFirstSecretValue = webMethod(Permissions.Anyone, () => {
16 return secrets.listSecretInfo()
17 .then((secrets) => {
18 return secrets.getSecretValue(secrets[0].name);
19 })
20 .catch((error) => {
21 console.error(error);
22 });
23});
24
Retrieve a name and get a secret's value

This example demonstrates how to delete the first secret stored in the Secrets Manager. First we retrieve the ID of the secret using the listSecretInfo() function. Then we get the value of the secret using the retrieved ID.

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { secrets } from 'wix-secrets-backend.v2';
3
4export const getFirstSecretValue = webMethod(Permissions.Anyone, () => {
5 return secrets.listSecretInfo()
6 .then((secrets) => {
7 return secrets.getSecretValue(secrets[0].name);
8 })
9 .catch((error) => {
10 console.error(error);
11 });
12});
13
14/*
15 * Returns a Promise that resolves to:
16 *
17 * "Fm8OfflH6bJOwWjenqAtLurLbkiMNvmhQHZV+118"
18 */
19
Get an API key and use it to fetch a JSON from a weather service

In this example, we use the Secrets API to get an API key from the Secrets Manager. We use the key in the backend to fetch a JSON with weather information from a 3rd-party service, and return the JSON to the frontend.

Copy Code
1/************************************
2 * Backend code - getWeather.web.js *
3 ************************************/
4
5import { Permissions, webMethod } from 'wix-web-module';
6import { secrets } from 'wix-secrets-backend.v2';
7import { getJSON } from 'wix-fetch';
8
9export const getWeatherJson = webMethod(Permissions.Anyone, async () => {
10 const secret = await secrets.getSecretValue('openWeatherApiKey');
11 return getJSON(`https://api.openweathermap.org/data/2.5/weather?q=London&units=metric&APPID=${secret}`);
12});
13
14/********************
15 * Frontend code *
16 ********************/
17
18import { getWeatherJson } from 'backend/getWeather.web';
19
20export async function getWeather_click(event) {
21 let json = await getWeatherJson();
22 $w('#weather').text = json.weather[0].description; // "mist"
23 $w('#temp').text = json.main.temp; // 9.4 (degrees Celsius)
24}
Get an API key and use it to send an email with the SendGrid npm interface

In this example, we added the SendGrid package to our site using the Package Manager. We created a backend function that gets an API key from the Secrets Manager, sets it as the SendGrid API key, and sends an email using the SendGrid service. We call the function from the client side when a site visitor submits email information via a form.

Copy Code
1/************************************
2 * Backend code - sendEmail.web.js *
3 ************************************/
4
5import { Permissions, webMethod } from 'wix-web-module';
6import { secrets } from 'wix-secrets-backend.v2';
7import sendGridMail from '@sendgrid/mail';
8
9export const sendEmail = webMethod(Permissions.Anyone, async (recipient, sender, subject, body) => {
10 const secret = await secrets.getSecretValue('SendGridApiKey');
11 sendGridMail.setApiKey(secret);
12 const message = {
13 'to': recipient,
14 'from': sender,
15 'subject': subject,
16 'text': body
17 };
18 sendGridMail.send(message);
19});
20
21/********************
22 * Frontend code *
23 ********************/
24
25import { sendEmail } from 'backend/sendEmail.web';
26
27export function sendEmailButton_click(event) {
28 sendEmail(
29 $w('#toEmail').value,
30 $w('#fromEmail').value,
31 $w('#subject').value,
32 $w('#emailContent').value
33 )
34 .then(() => {
35 console.log('Email sent');
36 })
37 .catch((error) => {
38 console.error(error);
39 })
40}