getSecret( )
Gets a secret by name.
Description
The getSecret()
function returns a Promise that resolves to the value of the secret
that was stored in the Secrets Manager with the given name.
Note: To prevent malicious users from accessing the value of your secret, don't return the value of the secret to client side. Only use the secret's value in the backend.
Syntax
function getSecret(name: string): Promise<string>
getSecret Parameters
NAME
TYPE
DESCRIPTION
The name of the secret to get the value of.
Returns
Fulfilled - The value of the secret with the given name. Rejected - Error message.
Return Type:
Was this helpful?
1import wixSecretsBackend from 'wix-secrets-backend';2import {getJSON} from 'wix-fetch';34export function getSomeJSON() {5 return wixSecretsBackend.getSecret("myApiKeyName")6 .then((secret) => {7 return getJSON(`https://someapi.com/api/someendpoint?apiKey=${secret}`);8 })9 .catch((error) => {10 console.error(error);11 });12}1314export function getFirstSecretValue() {15 return wixSecretsBackend.listSecretInfo()16 .then((secrets) => {17 return wixSecretsBackend.getSecret(secrets[0].name);18 })19 .catch((error) => {20 console.error(error);21 });22}
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.
1import wixSecretsBackend from 'wix-secrets-backend';23export function getFirstSecretValue() {4 return wixSecretsBackend.listSecretInfo()5 .then((secrets) => {6 return wixSecretsBackend.getSecret(secrets[0].name);7 })8 .catch((error) => {9 console.error(error);10 });11}1213/*14 * Returns a Promise that resolves to:15 *16 * "Fm8OfflH6bJOwWjenqAtLurLbkiMNvmhQHZV+118"17 */
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 client side.
1/************************************2 * Backend code - getWeather.jsw *3 ************************************/45import wixSecretsBackend from 'wix-secrets-backend';6import { getJSON } from 'wix-fetch';78export async function getWeatherJson() {9 const secret = await wixSecretsBackend.getSecret("openWeatherApiKey");10 return getJSON(`https://api.openweathermap.org/data/2.5/weather?q=London&units=metric&APPID=${secret}`);11}1213/********************14 * Client-side code *15 ********************/1617import { getWeatherJson } from 'backend/getWeather';1819export async function getWeather_click(event) {20 let json = await getWeatherJson();21 $w("#weather").text = json.weather[0].description; // "mist"22 $w("#temp").text = json.main.temp; // 9.4 (degrees Celsius)23}24
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.
1/************************************2 * Backend code - sendEmail.jsw *3 ************************************/45import wixSecretsBackend from 'wix-secrets-backend';6import sendGridMail from '@sendgrid/mail';78export async function sendEmail(recipient, sender, subject, body) {9 const secret = await wixSecretsBackend.getSecret("SendGridApiKey");10 sendGridMail.setApiKey(secret);11 const message = {12 "to": recipient,13 "from": sender,14 "subject": subject,15 "text": body16 };17 sendGridMail.send(message);18}1920/********************21 * Client-side code *22 ********************/2324import { sendEmail } from 'backend/sendEmail';2526export function sendEmailButton_click(event) {27 sendEmail(28 $w("#toEmail").value,29 $w("#fromEmail").value,30 $w("#subject").value,31 $w("#emailContent").value32 )33 .then(() => {34 console.log("Email sent");35 })36 .catch((error) => {37 console.error(error);38 })39}40