Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/ /
Administrar usuarios

Crear y gestionar claves API de usuario - Node.js SDK

Para crear un nuevo Clave API de usuario, pase un nombre que sea único entre todas las claves API del usuario a ApiKeyAuth.create().

const user = app.currentUser;
const key = await user.apiKeys.create("apiKeyName");

Importante

No se puede crear una clave API de usuario para una clave API de servidor o un usuario anónimo.

Advertencia

Store the API Key Value

El SDK solo devuelve el valor de la clave API del usuario al crearla. Asegúrese de almacenarla. key valor de forma segura para que puedas usarlo para iniciar sesión.

If you lose or do not store the key value there is no way to recover it. You will need to create a new user API key.

Para obtener un arreglo que liste todas las claves API de un usuario, llame a ApiKeyAuth.fetchAll().

To find a specific API key, pass the key's _id to ApiKeyAuth.fetch().

const user = app.currentUser;
// List all of a user's keys
const keys = await user.apiKeys.fetchAll();
// Get a specific key by its ID
const key = await user.apiKeys.fetch("5eb5931548d79bc784adf46e");

Para habilitar o deshabilitar una clave API de usuario, pasa el _id de la clave a ApiKeyAuth.enable() o ApiKeyAuth.disable(). Cuando una clave está deshabilitada, no puede usarse para iniciar sesión en nombre del usuario.

// Get the ID of a User API Key
const user = app.currentUser;
const apiKeys = await user.apiKeys.fetchAll();
const keyId = apiKeys[0]["_id"];
// Enable the User API Key
await user.apiKey.enable(keyId);
// Disable the User API Key
await user.apiKey.disable(keyId);

To permanently delete a user API, pass the key's _id to ApiKeyAuth.delete(). Deleted keys cannot be recovered.

// Get the ID of a User API Key
const user = app.currentUser;
const apiKeys = await user.apiKeys.fetchAll();
const keyId = apiKeys[0]["_id"];
// Delete the User API Key
await user.apiKey.delete(keyId);

Volver

Vincular identidades de usuario

En esta página