stytch
TypeScript icon, indicating that this package has built-in type declarations

12.23.0 • Public • Published

Stytch Node.js Library

The Stytch Node library makes it easy to use the Stytch user infrastructure API in server-side JavaScript applications.

It pairs well with the Stytch Web SDK or your own custom authentication flow.

This library is tested with all current LTS versions of Node - 18, and 20.

Install

npm install stytch
# or
yarn add stytch

Usage

You can find your API credentials in the Stytch Dashboard.

This client library supports all of Stytch's live products:

B2C

B2B

Shared

Example B2C usage

Create an API client:

const stytch = require("stytch");
// Or as an ES6 module:
// import * as stytch from "stytch";

const client = new stytch.Client({
  project_id: "project-live-c60c0abe-c25a-4472-a9ed-320c6667d317",
  secret: "secret-live-80JASucyk7z_G8Z-7dVwZVGXL5NT_qGAQ2I=",
});

Send a magic link by email:

client.magicLinks.email
  .loginOrCreate({
    email: "sandbox@stytch.com",
    login_magic_link_url: "https://example.com/authenticate",
    signup_magic_link_url: "https://example.com/authenticate",
  })
  .then((res) => console.log(res))
  .catch((err) => console.error(err));

Authenticate the token from the magic link:

client.magicLinks
  .authenticate({ token: "DOYoip3rvIMMW5lgItikFK-Ak1CfMsgjuiCyI7uuU94=" })
  .then((res) => console.log(res))
  .catch((err) => console.error(err));

Example B2B usage

Create an API client:

const stytch = require("stytch");
// Or as an ES6 module:
// import * as stytch from "stytch";

const client = new stytch.B2BClient({
  project_id: "project-live-c60c0abe-c25a-4472-a9ed-320c6667d317",
  secret: "secret-live-80JASucyk7z_G8Z-7dVwZVGXL5NT_qGAQ2I=",
});

Create an organization

client.organizations
  .create({
    organization_name: "Acme Co",
    organization_slug: "acme-co",
    email_allowed_domains: ["acme.co"],
  })
  .then((res) => console.log(res))
  .catch((err) => console.error(err));

Log the first user into the organization

client.magicLinks
  .loginOrSignup({
    organization_id: "organization-id-from-create-response-...",
    email_address: "admin@acme.co",
  })
  .then((res) => console.log(res))
  .catch((err) => console.error(err));

TypeScript support

This package includes TypeScript declarations for the Stytch API.

Request and response types will always follow the format $Vertical$Product$Method(Request|Response) - for example the B2BMagicLinksAuthenticateRequest maps to the B2B Authenticate Magic Link endpoint, while the B2CMagicLinksAuthenticateRequest maps to the B2C Authenticate Magic Link endpoint.

Handling Errors

Stytch errors always include an error_type field you can use to identify them:

client.magicLinks
  .authenticate({ token: "not-a-token!" })
  .then((res) => console.log(res))
  .catch((err) => {
    if (err.error_type === "invalid_token") {
      console.log("Whoops! Try again?");
    }
  });

Learn more about errors in the docs.

Customizing the HTTPS Agent

The Stytch client uses undici, the Node fetch implementation. You can pass a custom undici Dispatcher to the client for use in requests. For example, you can enable HTTPS Keep-Alive to avoid the cost of establishing a new connection with the Stytch servers on every request.

const dispatcher = new undici.Agent({
  keepAliveTimeout: 6e6, // 10 minutes in MS
  keepAliveMaxTimeout: 6e6, // 10 minutes in MS
});

const client = new stytch.Client({
  project_id: "project-live-c60c0abe-c25a-4472-a9ed-320c6667d317",
  secret: "secret-live-80JASucyk7z_G8Z-7dVwZVGXL5NT_qGAQ2I=",
  dispatcher,
});

Documentation

See example requests and responses for all the endpoints in the Stytch API Reference.

Follow one of the integration guides or start with one of our example apps.

Support

If you've found a bug, open an issue!

If you have questions or want help troubleshooting, join us in Slack or email support@stytch.com.

If you've found a security vulnerability, please follow our responsible disclosure instructions.

Development

See DEVELOPMENT.md

Code of Conduct

Everyone interacting in the Stytch project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
10.0.1-debug0debug
12.23.05,114latest
12.11.2-alpha.01next

Version History

VersionDownloads (Last 7 Days)Published
12.23.05,114
12.22.0720
12.21.01,546
12.20.0313
12.19.06,757
12.18.11
12.18.013
12.17.054
12.16.015
12.15.02
12.14.01,004
12.13.066
12.12.22,075
12.12.168
12.12.021
12.11.2-alpha.01
12.11.016
12.10.0391
12.9.02
12.8.0110
12.7.02,915
12.6.01
12.5.0166
12.4.03,556
12.3.11,853
12.3.0175
12.2.012
12.1.01
12.0.020
11.11.01,696
11.10.0543
11.9.01
11.8.0411
11.7.0869
11.6.02
11.5.01,344
11.4.2397
11.4.184
11.3.00
11.2.017
11.1.01
11.0.00
10.20.02,506
10.19.00
10.18.01,367
10.17.02
10.16.010
10.15.139
10.15.01
10.14.0111
10.13.098
10.12.0148
10.11.08
10.10.01
10.9.03
10.8.047
10.7.053
10.6.00
10.5.08
10.4.069
10.3.021
10.2.04
10.1.01
10.0.260
10.0.1-debug0
10.0.1231
10.0.00
9.1.0410
9.0.63
9.0.51
9.0.40
9.0.30
9.0.21
9.0.11
9.0.02
9.0.0-rc.10
9.0.0-rc.01
8.4.2286
8.4.11
8.4.0127
8.3.01
8.2.00
8.1.01
8.0.034
7.5.1508
7.5.00
7.4.01
7.3.10
7.3.00
7.2.085
7.1.31
7.1.20
7.1.10
7.1.00
7.0.10
7.0.00
6.2.271
6.2.10
6.2.01
6.1.32
6.1.20
6.1.10
6.1.01
6.0.90
6.0.80
6.0.70
6.0.60
6.0.51
6.0.40
6.0.30
6.0.211
6.0.10
6.0.00
6.0.0-next.00
5.18.1752
5.18.02
5.17.0188
5.16.10
5.16.05
5.15.10
5.15.00
5.14.00
5.13.0258
5.12.20
5.12.11
5.12.00
5.11.14
5.11.00
5.10.20
5.10.00
5.9.00
5.8.011
5.7.00
5.6.00
5.5.00
5.4.00
5.3.00
5.2.10
5.2.00
5.1.50
5.1.40
5.1.30
5.1.20
5.1.00
5.0.00
4.0.00
3.13.1504
3.13.00
3.12.10
3.12.00
3.11.20
3.11.10
3.11.01
3.10.20
3.10.10
3.9.00
3.8.00
3.7.0128
3.6.1264
3.6.00
3.5.10
3.5.01
3.4.00
3.3.00
3.2.10
3.2.00
3.1.00
3.0.20
3.0.10
3.0.00
3.0.0-beta.40
3.0.0-beta.30
3.0.0-beta.20
3.0.0-beta.10
2.0.10
1.2.40
2.0.00
1.2.30
1.2.20
1.2.10
1.2.00
1.1.80
1.1.70
1.1.50
1.1.40
1.1.30
1.1.20
1.1.10
1.1.00
1.0.10
1.0.00
0.0.11

Package Sidebar

Install

npm i stytch

Weekly Downloads

40,812

Version

12.23.0

License

MIT

Unpacked Size

976 kB

Total Files

159

Last publish

Collaborators

  • julianna-stytch
  • marygruen
  • reed-stytch
  • grace-stytch
  • alex-stytch
  • danny-stytch
  • max-stytch
  • mia-stytch
  • jennifer-stytch
  • vincent-stytch
  • nicole-stytch
  • lydia-stytch
  • ollie-stytch
  • lgore-stytch