### Install Project Dependencies Source: https://github.com/stytchauth/stytch-node/blob/main/DEVELOPMENT.md Installs all necessary project dependencies using npm. This is the first step after cloning the repository. ```bash npm install ``` -------------------------------- ### Run Next.js Development Server Source: https://github.com/stytchauth/stytch-node/blob/main/test-runtimes/nextjs/README.md Commands to start the local development server for a Next.js project. These commands are typically executed from the project's root directory using various package managers. ```bash npm run dev # or yarn dev # or pnpm dev # or bun dev ``` -------------------------------- ### Install Dependencies Source: https://github.com/stytchauth/stytch-node/blob/main/test-runtimes/bun/README.md Installs all project dependencies using the Bun package manager. This command downloads and sets up all necessary libraries for the project. ```bash bun install ``` -------------------------------- ### Install Stytch Node.js Library Source: https://github.com/stytchauth/stytch-node/blob/main/README.md Instructions for installing the Stytch Node.js library using npm or yarn. ```bash npm install stytch # or yarn add stytch ``` -------------------------------- ### Configure and Run Integration Tests Source: https://github.com/stytchauth/stytch-node/blob/main/DEVELOPMENT.md Sets up environment variables for Stytch project credentials and then runs integration tests. Ensure `RUN_INTEGRATION_TESTS` is set to `1` to un-skip these tests. ```bash export PROJECT_ID='project-test-...' export SECRET='secret-test-...' export RUN_INTEGRATION_TESTS=1 npm test ``` -------------------------------- ### Initialize Stytch Client (B2C) Source: https://github.com/stytchauth/stytch-node/blob/main/README.md Example of creating a Stytch client instance for B2C authentication flows using your project ID and secret. ```javascript 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=", }); ``` -------------------------------- ### Initialize Stytch B2B Client Source: https://github.com/stytchauth/stytch-node/blob/main/README.md Example of creating a Stytch client instance specifically for B2B authentication flows, using project ID and secret. ```javascript 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=", }); ``` -------------------------------- ### Run Project Source: https://github.com/stytchauth/stytch-node/blob/main/test-runtimes/bun/README.md Executes the main TypeScript file of the project using the Bun runtime. This command starts the application or script defined in index.ts. ```bash bun run index.ts ``` -------------------------------- ### Run Unit Tests Source: https://github.com/stytchauth/stytch-node/blob/main/DEVELOPMENT.md Executes the unit test suite for the library. This command provides confidence that changes work as intended. ```bash npm test ``` -------------------------------- ### Login/Signup User into Organization (B2B) Source: https://github.com/stytchauth/stytch-node/blob/main/README.md Example of initiating a login or signup flow for a user into a specific organization using magic links in B2B scenarios. Requires a B2B Stytch client instance. ```javascript 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)); ``` -------------------------------- ### Build Project Files Source: https://github.com/stytchauth/stytch-node/blob/main/DEVELOPMENT.md Generates the distributed JavaScript files and TypeScript declaration files. These generated files should be committed. ```bash npm run build ``` -------------------------------- ### Start Remix App in Development Mode Source: https://github.com/stytchauth/stytch-node/blob/main/test-runtimes/remix/README.md This command initiates the Remix application server in development mode. It monitors file changes and automatically rebuilds assets, providing a live-reloading development environment. ```sh npm run dev ``` -------------------------------- ### Authenticate Magic Link Token (B2C) Source: https://github.com/stytchauth/stytch-node/blob/main/README.md Example of authenticating a token received from a magic link to verify a user's identity in B2C flows. Requires a Stytch client instance. ```javascript client.magicLinks .authenticate({ token: "DOYoip3rvIMMW5lgItikFK-Ak1CfMsgjuiCyI7uuU94=" }) .then((res) => console.log(res)) .catch((err) => console.error(err)); ``` -------------------------------- ### Initialize Stytch Client Source: https://github.com/stytchauth/stytch-node/blob/main/test-runtimes/README.md Demonstrates how to initialize the Stytch client in a JavaScript environment. It requires project_id and secret credentials, which can be found in the Stytch dashboard. The example uses specific test credentials that trigger a well-known error message. ```javascript // Find these values at https://stytch.com/dashboard/api-keys // These ones will trigger a well-known erorr message const client = new stytch.Client({ project_id: "project-live-c60c0abe-c25a-4472-a9ed-320c6667d317", secret: "secret-live-80JASucyk7z_G8Z-7dVwZVGXL5NT_qGAQ2I=", }); ``` -------------------------------- ### Next.js API Route Structure Source: https://github.com/stytchauth/stytch-node/blob/main/test-runtimes/nextjs/README.md Information on how API routes are handled in Next.js. Files within the `pages/api` directory are automatically mapped to API routes, allowing for serverless backend functionality. ```typescript // Example: pages/api/hello.ts import type { NextApiRequest, NextApiResponse } from 'next' type Data = { name: string } export default function handler(req: NextApiRequest, res: NextApiResponse) { res.status(200).json({ name: 'John Doe' }) } ``` -------------------------------- ### Build and Run Remix App for Production Source: https://github.com/stytchauth/stytch-node/blob/main/test-runtimes/remix/README.md These commands are used to prepare the Remix application for deployment. First, build the app for production, then run the built application in production mode. ```sh npm run build npm start ``` -------------------------------- ### Create Organization (B2B) Source: https://github.com/stytchauth/stytch-node/blob/main/README.md Demonstrates creating a new organization within Stytch for B2B use cases. Requires a B2B Stytch client instance. ```javascript 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)); ``` -------------------------------- ### Send Magic Link by Email (B2C) Source: https://github.com/stytchauth/stytch-node/blob/main/README.md Demonstrates sending a magic link to a user's email for login or signup in B2C flows. Requires a Stytch client instance. ```javascript 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)); ``` -------------------------------- ### Customize HTTPS Agent with Undici Source: https://github.com/stytchauth/stytch-node/blob/main/README.md Shows how to configure the Stytch Node.js client with a custom `undici` Dispatcher to manage HTTPS connections, such as enabling Keep-Alive for improved performance by reusing connections. This involves creating an `undici.Agent` and passing it during client initialization. ```javascript 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, }); ``` -------------------------------- ### TypeScript Type Naming Convention Source: https://github.com/stytchauth/stytch-node/blob/main/README.md Explains the naming convention for TypeScript request and response types in the Stytch Node.js library, mapping them to API endpoints. ```APIDOC TypeScript Type Naming Convention: Request and response types follow the format `$Vertical$Product$Method(Request|Response)`. Examples: - `B2BMagicLinksAuthenticateRequest` maps to the B2B [Authenticate Magic Link](https://stytch.com/docs/b2b/api/authenticate-magic-link) endpoint. - `B2CMagicLinksAuthenticateRequest` maps to the B2C [Authenticate Magic Link](https://stytch.com/docs/api/authenticate-magic-link) endpoint. ``` -------------------------------- ### Handle Stytch Errors in Node.js Source: https://github.com/stytchauth/stytch-node/blob/main/README.md Demonstrates how to catch errors from the Stytch client and identify specific error types using the `error_type` field. This is useful for implementing custom error handling logic, such as prompting the user to try again for invalid tokens. ```javascript 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?"); } }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.