Skip to main content

Quickstart (Backend)

This guide will help you make your first calls to Privy from your backend using the privy-node client. This is the fastest way to get user data off of your stack.

info

Do you not have a backend? Are you unsure about which client to use? Refer to Picking a client to understand which client is right for you.

0. Prerequisitesโ€‹

If you want to follow along, you'll need:

  • A Privy account. If you don't have one, get one here.
  • Node.js (v14 or later) and npm installed.

1. Install the SDKโ€‹

Install the Node SDK with a single npm command:

npm install @privy-io/privy-node
info

Privy runs on Node.js >=v14

2. Configure fields and permissions in the consoleโ€‹

The Privy console is the easiest way to manage the data fields and permissions for your app.

Create a new field in the console's Fields page. Add useful attributes like a human-readable name and a description.

Take note of the auto-generated field ID. This identifier is how you'll modify and access this field via the Privy API.

You can leave the permissions as the defaults for now, or read more about how to set up default permissions with the console.

3. Generate API keysโ€‹

Next, head over to the Settings page in the console and click "Generate New Keys" to get your API key and secret. You'll need both of these to instantiate the node client.

caution

Privy does not store your secret key. Therefore, once generated, it is up to you to securely store this value. Do not share the secret with anyone.

4. Putting and getting dataโ€‹

Import the node client and initialize the client with your API key and secret.

import {PrivyClient} from '@privy-io/privy-node';

const PRIVY_API_KEY = '<retrieved from the Privy console>';
const PRIVY_API_SECRET = '<retrieved from the Privy console>';

const client = new PrivyClient(PRIVY_API_KEY, PRIVY_API_SECRET);

Storing data with Privy is as easy as calling client.put(<user ID>, <data>).

Any string value can be used for the user ID. Many dapps choose to reference users by their wallet address, e.g. "0xAb5.." in this example.

// '0xAb5..' is the user ID used to reference data for that user.
// 'first-name' is the field ID used to reference the field we created in the console.
const result = await client.put(
'0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B',
'first-name',
'Vitalik',
);

See the complete documentation for the node client's put() method here.

Retrieving data is just as easy as putting data.

const result = await client.get('0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B', 'first-name');
console.log(result.text()); // Vitalik

See the complete documentation for the node client's get() method here.

Voila! User data no longer lives on your stack. Privy handles all of the encryption, access control, and data storage, so that you don't have to.

tip

You can find a complete Node script with the code snippets above here.

5. Explore next stepsโ€‹

It's really that simple - with just a few lines of code, Privy handles your user data for you. But there's so much more that Privy can do!

  • Want to access user data directly from your dapp frontend? ๐Ÿค– Learn more about privy-browser in Picking a client.
  • Want to know how Privy works under the hood? ๐Ÿ”’ Check out our Architecture and Security
  • Want to check out demos built with Privy? ๐ŸŽฉ Head to our demos page