Error authenticating Email OTP in test environment

I’m trying to authenticate via Email OTP in the test environment. After calling loginOrCreate I then pass in the returned email_id as the method_id to the authenticate method and get the error that the email could not be found. I’m using the predefined sandbox@stytch.com email address so I don’t understand how it cannot be found when the first call succeeds. Here is a trace of the two calls and their inputs and responses:

calling stytch.otps.email.loginOrCreate
request:
{
  "email": "sandbox@stytch.com"
}
response:
 {
  "email_id": "email-test-23873e89-d4ed-4e92-b3b9-e5c7198fa286",
  "request_id": "request-id-test-7bf45c7b-a339-4ccf-81d6-1ff3f2453f1e",
  "status_code": 200,
  "user_created": false,
  "user_id": "user-test-e3795c81-f849-4167-bfda-e4a6e9c280fd"
}

calling stytch.otps.authenticate
request:
 {
  "method_id": "email-test-23873e89-d4ed-4e92-b3b9-e5c7198fa286",
  "code": "123456",
  "session_duration_minutes": 10080
}
err: {
  "type": "StytchError",
  "message": "{\"status_code\":404,\"request_id\":\"request-id-test-bd38b4df-3643-4979-a41f-07322ad189a3\",\"error_type\":\"email_not_found\",\"error_message\":\"Email could not be found.\",\"error_url\":\"https://stytch.com/docs/api/errors/404#email_not_found\"}",
  "stack":
      Error: {"status_code":404,"request_id":"request-id-test-bd38b4df-3643-4979-a41f-07322ad189a3","error_type":"email_not_found","error_message":"Email could not be found.","error_url":"https://stytch.com/docs/api/errors/404#email_not_found"}
          at request (/Volumes/OWC4TB/projects/lyfeplan/node_modules/.pnpm/stytch@12.32.0/node_modules/stytch/dist/shared/index.js:46:11)
          at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
          at async Object.<anonymous> (/Volumes/OWC4TB/projects/lyfeplan/apps/api/src/modules/auth/auth.routes.ts:97:18)
  "status_code": 404,
  "request_id": "request-id-test-bd38b4df-3643-4979-a41f-07322ad189a3",
  "error_type": "email_not_found",
  "error_message": "Email could not be found.",
  "error_url": "https://stytch.com/docs/api/errors/404#email_not_found"
}

Hey David – thanks for posting!

In order for our sandbox values to work as expected, you’ll want to use 000000 as your OTP code (rather than 123456). Our sandbox values are special-cased, so they work a bit differently than normal email/ phone factors.

Happy to help with anything else that comes up!

Great - that fixed it. It would be nice if the error said something like “Invalid code” instead of “Email could not be found”. I hope this is a one-off and in general, the error messages are more indicative of the actual problem.

That makes sense! This is definitely unique to our sandbox values – in general, we return error messages that are specific to the issue (so email_not_found when the email doesn’t exist in our system and otp_code_not_found when the OTP code is incorrect). Sorry for the confusion there!

1 Like