Docs

Create user metadata

These methods on the User object help you create data for the user, such as createEmailAddress() and createPhoneNumber().

The following examples assume that you have followed the quickstart in order to add Clerk to your JavaScript application.

createEmailAddress()

Adds an email address for the user. A new EmailAddress will be created and associated with the user.

Warning

Email address must be enabled as an identifier in your Clerk settings for this method to work. See the Identifiers section to learn more.

function createEmailAddress(params: CreateEmailAddressParams): Promise<EmailAddress>
  • Name
    email
    Type
    string
    Description

    The value of the email address

Example

The following example adds test@test.com as an email address for the user. If the user is not signed in, the user will be prompted to sign in.

main.js
import { Clerk } from '@clerk/clerk-js'

// Initialize Clerk with your Clerk Publishable Key
const pubKey = import.meta.env.VITE_CLERK_PUBLISHABLE_KEY

const clerk = new Clerk(pubKey)
await clerk.load()

if (clerk.user) {
  const email = 'test@test.com'

  clerk.user
    .createEmailAddress({ email })
    .then((response) => console.log(response))
    .catch((error) => console.log('An error occurred:', error.errors))
} else {
  document.getElementById('app').innerHTML = `
    <div id="sign-in"></div>
  `

  const signInDiv = document.getElementById('sign-in')

  clerk.mountSignIn(signInDiv)
}

createPhoneNumber()

Adds a phone number for the user. A new PhoneNumber will be created and associated with the user.

Warning

Phone number must be enabled as an identifier in your Clerk settings for this method to work. See the Identifiers section to learn more.

function createPhoneNumber(params: CreatePhoneNumberParams): Promise<PhoneNumber>
  • Name
    phoneNumber
    Type
    string
    Description

    The value of the phone number, in E.164 format.

Example

The following example adds 15551234567 as a phone number for the user. If the user is not signed in, the user will be prompted to sign in.

main.js
import { Clerk } from '@clerk/clerk-js'

// Initialize Clerk with your Clerk Publishable Key
const pubKey = import.meta.env.VITE_CLERK_PUBLISHABLE_KEY

const clerk = new Clerk(pubKey)
await clerk.load()

if (clerk.user) {
  const phoneNumber = '15551234567'

  clerk.user
    .createPhoneNumber({ phoneNumber })
    .then((response) => console.log(response))
    .catch((error) => console.log('An error occurred:', error.errors))
} else {
  document.getElementById('app').innerHTML = `
    <div id="sign-in"></div>
  `

  const signInDiv = document.getElementById('sign-in')

  clerk.mountSignIn(signInDiv)
}

createWeb3Wallet()

Adds a Web3 wallet for the user. A new Web3Wallet will be created and associated with the user.

Warning

A Web3 provider must be enabled in your Clerk settings for this method to work. See the Web3 authentication section to learn more.

function createWeb3Wallet(params: CreateWeb3WalletParams): Promise<Web3Wallet>
  • Name
    web3Wallet
    Type
    string
    Description

    The Web3 wallet address, made up of 0x + 40 hexadecimal characters.

Example

The following example adds 0x0123456789ABCDEF0123456789ABCDEF01234567 as a Web3 wallet for the user. If the user is not signed in, the user will be prompted to sign in.

main.js
import { Clerk } from '@clerk/clerk-js'

// Initialize Clerk with your Clerk Publishable Key
const pubKey = import.meta.env.VITE_CLERK_PUBLISHABLE_KEY

const clerk = new Clerk(pubKey)
await clerk.load()

if (clerk.user) {
  const web3Wallet = '0x0123456789ABCDEF0123456789ABCDEF01234567'

  clerk.user
    .createWeb3Wallet({ web3Wallet })
    .then((response) => console.log(response))
    .catch((error) => console.log('An error occurred:', error.errors))
} else {
  document.getElementById('app').innerHTML = `
    <div id="sign-in"></div>
  `

  const signInDiv = document.getElementById('sign-in')

  clerk.mountSignIn(signInDiv)
}

createExternalAccount()

Adds an external account for the user. A new ExternalAccount will be created and associated with the user. This method is useful if you want to allow an already signed-in user to connect their account with an external provider, such as Facebook, GitHub, etc., so that they can sign in with that provider in the future.

Warning

The social provider that you want to connect to must be enabled in your Clerk settings for this method to work. See the Social connections (OAuth) section to learn more.

function createExternalAccount(params: CreateExternalAccountParams): Promise<ExternalAccount>
  • Name
    strategy
    Type
    OAuthStrategy
    Description

    The strategy corresponding to the OAuth provider. For example: oauth_facebook, oauth_github, etc.

  • Name
    redirectUrl?
    Type
    string
    Description

    The URL to redirect back to once the OAuth flow has succeeded or failed.

  • Name
    additionalScopes?
    Type
    string[]
    Description

    Any additional scopes you would like your user to be prompted to approve.

Example

After calling createExternalAccount, the initial state of the returned ExternalAccount will be unverified. To initiate the connection with the external provider, redirect the user to the externalAccount.verification.externalVerificationRedirectURL contained in the result of createExternalAccount.

Upon return, inspect within the user.externalAccounts the entry that corresponds to the requested strategy:

  • If the connection succeeded, then externalAccount.verification.status will be verified.
  • If the connection failed, then the externalAccount.verification.status will not be verified and the externalAccount.verification.error will contain the error encountered, which you can present to the user. To learn more about the properties available on verification, see the verification reference.

The following example demonstrates how to add a Notion account as an external account for the user. When the user selects the "Add Notion as a social connection" button, the user will be redirected to Notion to connect their account. After connecting their account, they will be redirected to the / route of your application, and the status of the connection will be displayed.

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" type="image/svg+xml" href="/vite.svg" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Clerk + JavaScript App</title>
  </head>
  <body>
    <div id="app"></div>

    <p>
      Notion verification status:
      <span id="notion-status"></span>
    </p>
    <button id="add-notion">Add Notion as a social connection</button>

    <script type="module" src="/main.js"></script>
  </body>
</html>
main.js
import { Clerk } from '@clerk/clerk-js'

// Initialize Clerk with your Clerk Publishable Key
const pubKey = import.meta.env.VITE_CLERK_PUBLISHABLE_KEY

const clerk = new Clerk(pubKey)
await clerk.load()

if (clerk.user) {
  // Find the external account for the provider
  const externalAccount = clerk.user.externalAccounts.find((p) => p.provider === 'notion')
  // If the external account exists, display its status
  document.getElementById('notion-status').innerHTML = externalAccount.verification.status

  // When the button is clicked, initiate the connection with the provider
  document.getElementById('add-notion').addEventListener('click', async () => {
    clerk.user
      .createExternalAccount({ strategy: 'oauth_notion', redirect_url: '/' })
      .then((externalAccount) => {
        window.location.href = externalAccount.verification.externalVerificationRedirectURL
      })
      .catch((error) => {
        console.log('An error occurred:', error.errors)
      })
  })
} else {
  document.getElementById('app').innerHTML = `
    <div id="sign-in"></div>
  `

  const signInDiv = document.getElementById('sign-in')

  clerk.mountSignIn(signInDiv)
}

Feedback

What did you think of this content?

Last updated on