Magic Link

The fastest way to securely enable authentication - passwordless sign-in via email in a couple lines of code.


Managing password authentication entails significant overhead for engineering teams and opens up many risks for breaches. Magic Link from WorkOS gives you the ability to build a secure passwordless authentication flow utilizing email. Send users a one-time-use link and let them sign in with a single click.

Magic Link sets you up to handle Single Sign-On via the WorkOS SSO API. The redirect URI / profile retrieval process is identical between Magic Link and SSO.

Magic Link is not a session management solution - instead it is an authentication solution. You as the developer have the ability to manage user sessions as you see fit. This means you determine how long a session is valid for, as well as how to store and invalidate a session.

Magic Links are single use, meaning that they will immediately expire after they are first clicked. If end users have security checks included with their email provider that test all embedded URLs, it can cause the Magic Links to expire before they reach the user's inbox. To work around this, we recommend that end users add these Magic Link emails to an allowlist in order to bypass these security checks.

Creating an IP Allowlist

WorkOS makes use of Cloudflare to ensure security and reliability of all operations. If you are looking to create a list of allowed IP addresses for incoming requests, you can use the IP Ranges listed in the Cloudflare documentation.

What you’ll build

In this guide, we’ll take you from learning about passwordless authentication all the way through to building production-ready passwordless authentication flows with the WorkOS Magic Link API.

Sign in to your WorkOS Dashboard account to see code examples pre-filled with your API keys and resource IDs.

This guide will show you how to:


Add Magic Link to your app.


Configure a redirect URI.


Create a new Magic Link Connection.

Before getting started

To get the most out of this guide, you’ll need:

API Object Definitions



Passwordless Session

A Passwordless Session is an object that represents a passwordless authentication session.


A Profile is an object that represents an authenticated user. The Profile object contains information relevant to a user in the form of normalized and raw attributes.


Add Magic Link to your app

Let’s start by building the Magic Link authentication workflow into your app.

Install the WorkOS SDK of your choice

WorkOS offers native SDKs in several popular programming languages. Choose a language below to see instructions in your application’s language.

  • Node.js


  • Ruby


  • Go


  • Python


  • PHP


  • Laravel


  • .NET


  • Java


Request an SDK

Don't see an SDK you need? Contact us to request an SDK!

Magic Link Example Application

Node.js Example Application

Install the SDK using the command below.

Command Line

npm install @workos-inc/node
You can also download the workos-node source code from GitHub.

Set environment variables

As a best practice, your WorkOS API key should be kept secret and set as an environment variable on process start. The SDK is able to read the key automatically if you store it in an environment variable named WORKOS_API_KEY; otherwise, you will need to set it manually. The Client ID should also be set dynamically based on the release environment.

Environment Variables


Add a callback endpoint

Let’s first add the redirect endpoint which will handle the callback from WorkOS after a user has authenticated via a magic link. This endpoint should exchange the authorization code (valid for 10 minutes) returned by WorkOS with the authenticated user’s Profile.

The redirect URI must use HTTPS.

Callback Endpoint

1const express = require('express');
2const { WorkOS } = require('@workos-inc/node');
4const app = express();
6const workos = new WorkOS(process.env.WORKOS_API_KEY);
7const clientID = process.env.WORKOS_CLIENT_ID;
9app.get('/callback', async (req, res) => {
10 const { code } = req.query;
12 const { profile } = await workos.sso.getProfileAndToken({
13 code,
14 clientID,
15 });
17 // Use the information in `profile` for further business logic.
19 res.redirect('/');

Create a Passwordless Session and email the user

Create a Passwordless Session to generate an authentication link for a user. An authentication link is valid for 15 minutes and can be sent via the WorkOS API or using your own email service.

You can use the optional state parameter to encode arbitrary information to help restore application state between redirects.

You can use the optional redirect_uri parameter to override the default Redirect URI set in the dashboard.

A Magic Link Connection will be automatically created if there isn’t already one for the domain specified when creating a new Passwordless Session.

Use the WorkOS API to send the authentication link to the user. The email sent will be WorkOS branded.

Create a Passwordless Session

1const express = require('express');
2const { WorkOS } = require('@workos-inc/node');
4const app = express();
6const workos = new WorkOS(process.env.WORKOS_API_KEY);
7'/passwordless-auth', async (req, res) => {
9 const email = // ... Email of the user to authenticate
11 const session = await workos.passwordless.createSession({
12 email,
13 type: 'MagicLink',
14 });
15 await workos.passwordless.sendSession(;
17 // Finally, redirect to a "Check your email" page

Configure a redirect URI

You should set a redirect URI (i.e. the callback endpoint from Add a callback endpoint) via the WorkOS Dashboard - be sure not to include wildcard subdomains or query parameters.

Go live checklist

Make sure you're ready to go live to production by going through this checklist.

  • Unlock your Production environment by adding your billing information.

    Only enterprise connections in your Production environment will be charged. Any Google OAuth or Magic Link connections in Production will be free.

  • Configure a production redirect URI in your Production Project.
  • Secure your Production Project's API key.
  • Ensure that your application can receive redirects from WorkOS.

    Depending on your network architecture, you may need to allowlist incoming redirect traffic from

    WorkOS currently cannot promise that redirect traffic will originate from a static set of IP addresses.

Frequently asked questions

Can I customize the email sent via Magic Link?

Yes, you can use your own email service and custom branded email template to send the authentication link to the user. See the custom email code snippet in the create passwordless session section for an example.