create_user/
create_user.rs

1//! Create User Example
2//!
3//! This example demonstrates how to create a new user in Privy with linked accounts.
4//! It shows how to:
5//! - Initialize a Privy client with app credentials
6//! - Create a user with email and custom JWT linked accounts
7//! - Handle the response containing the new user data
8//!
9//! ## Required Environment Variables
10//! - `PRIVY_APP_ID`: Your Privy staging app ID
11//! - `PRIVY_APP_SECRET`: Your Privy staging app secret
12//!
13//! ## Usage
14//! ```bash
15//! cargo run --example create_user
16//! ```
17
18use anyhow::Result;
19use privy_rs::{
20    PrivyClient,
21    generated::types::{
22        CreateUserBody, LinkedAccountCustomJwtInput, LinkedAccountCustomJwtInputType,
23        LinkedAccountEmailInput, LinkedAccountEmailInputType, LinkedAccountInput,
24    },
25};
26use tracing_subscriber::EnvFilter;
27
28#[tokio::main]
29async fn main() -> Result<()> {
30    tracing_subscriber::fmt()
31        .with_env_filter(
32            EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")),
33        )
34        .init();
35
36    // Initialize client from environment variables
37    let client = PrivyClient::new_from_env()?;
38
39    tracing::info!("initialized privy client from environment");
40
41    let user = client
42        .users()
43        .create(&CreateUserBody {
44            linked_accounts: vec![
45                LinkedAccountInput::EmailInput(LinkedAccountEmailInput {
46                    address: "alex@arlyon.dev".into(),
47                    type_: LinkedAccountEmailInputType::Email,
48                }),
49                LinkedAccountInput::CustomJwtInput(LinkedAccountCustomJwtInput {
50                    custom_user_id: "alex@arlyon.dev".try_into().unwrap(),
51                    type_: LinkedAccountCustomJwtInputType::CustomAuth,
52                }),
53            ],
54            custom_metadata: None,
55            wallets: vec![],
56        })
57        .await?;
58
59    tracing::info!("got new user: {:?}", user);
60
61    Ok(())
62}