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

11.10.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
9.0.0-rc.10next
10.0.1-debug0debug
11.10.04,566latest

Version History

VersionDownloads (Last 7 Days)Published
11.10.04,566
11.9.017
11.8.0434
11.7.01,687
11.6.0380
11.5.01,490
11.4.2975
11.4.1428
11.3.015
11.2.09
11.1.00
11.0.095
10.20.03,582
10.19.00
10.18.04,275
10.17.0491
10.16.042
10.15.1605
10.15.06
10.14.0255
10.13.0198
10.12.0109
10.11.00
10.10.01
10.9.05
10.8.016
10.7.0144
10.6.00
10.5.0552
10.4.077
10.3.0258
10.2.015
10.1.01
10.0.255
10.0.1-debug0
10.0.187
10.0.00
9.1.02,152
9.0.613
9.0.51
9.0.4660
9.0.30
9.0.23
9.0.10
9.0.048
9.0.0-rc.10
9.0.0-rc.00
8.4.2912
8.4.114
8.4.0157
8.3.00
8.2.02
8.1.00
8.0.09
7.5.16,868
7.5.00
7.4.00
7.3.10
7.3.00
7.2.042
7.1.31
7.1.20
7.1.183
7.1.01
7.0.111
7.0.00
6.2.2172
6.2.10
6.2.00
6.1.38
6.1.20
6.1.10
6.1.00
6.0.90
6.0.80
6.0.70
6.0.61
6.0.50
6.0.40
6.0.30
6.0.220
6.0.10
6.0.00
6.0.0-next.00
5.18.1594
5.18.00
5.17.058
5.16.10
5.16.040
5.15.10
5.15.01
5.14.00
5.13.065
5.12.21
5.12.1133
5.12.00
5.11.12
5.11.00
5.10.20
5.10.00
5.9.00
5.8.01
5.7.00
5.6.00
5.5.00
5.4.00
5.3.0150
5.2.10
5.2.00
5.1.50
5.1.41
5.1.30
5.1.20
5.1.00
5.0.00
4.0.00
3.13.124
3.13.00
3.12.11
3.12.00
3.11.20
3.11.10
3.11.0496
3.10.20
3.10.10
3.9.00
3.8.00
3.7.00
3.6.10
3.6.00
3.5.10
3.5.00
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.10

Package Sidebar

Install

npm i stytch

Weekly Downloads

33,615

Version

11.10.0

License

MIT

Unpacked Size

852 kB

Total Files

142

Last publish

Collaborators

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