How to send a custom response by overriding the API Interface
Let's take an example of sending a custom response for the /auth/signup/email/exists GET
API (does email exist).
We need to first override the function for that API (emailExistsGET
) and then use the response object in the input
param to send a custom response.
The function signature expects an return type that has a certain shape, therefore, we must still return a valid response object from the function, but that will be ignored since you have already sent a response to the client.
- NodeJS
- GoLang
- Python
- Other Frameworks
Important
For other backend frameworks, you can follow our guide on how to spin up a separate server configured with the SuperTokens backend SDK to authenticate requests and issue session tokens.
import Passwordless from "supertokens-node/recipe/passwordless";
Passwordless.init({
override: {
apis: (originalImplementation) => {
return {
...originalImplementation,
emailExistsGET: async function (input) {
// we can send a custom response like this:
input.options.res.setStatusCode(200); // or any other status code
input.options.res.sendJSONResponse({
message: "my custom response",
//...
})
// this return doesn't matter. But we must do it
// cause the function signature expects a response.
return {
status: "OK",
exists: false
};
}
}
}
}
})
import (
"encoding/json"
"github.com/supertokens/supertokens-golang/recipe/passwordless/plessmodels"
"github.com/supertokens/supertokens-golang/recipe/passwordless"
"github.com/supertokens/supertokens-golang/supertokens"
)
func main() {
passwordless.Init(plessmodels.TypeInput{
Override: &plessmodels.OverrideStruct{
APIs: func(originalImplementation plessmodels.APIInterface) plessmodels.APIInterface {
(*originalImplementation.EmailExistsGET) = func(email, tenantId string, options plessmodels.APIOptions, userContext supertokens.UserContext) (plessmodels.EmailExistsGETResponse, error) {
// we create our custom response.
options.Res.Header().Set("Content-Type", "application/json; charset=utf-8")
options.Res.WriteHeader(200)
responseJson := map[string]interface{}{
"message": "My custom response",
// ...
}
bytes, _ := json.Marshal(responseJson)
options.Res.Write(bytes)
// this return doesn't matter. But we must do it
// cause the function signature expects a response.
return plessmodels.EmailExistsGETResponse{
OK: &struct{ Exists bool }{
Exists: false,
},
}, nil
}
return originalImplementation
},
},
})
}
from supertokens_python.recipe import passwordless
from supertokens_python.recipe.passwordless.interfaces import APIOptions, EmailExistsGetOkResult
from typing import Dict, Any
from supertokens_python.recipe.passwordless.interfaces import APIInterface
def override_passwordless_apis(original_implementation: APIInterface):
async def email_exists_get(email: str, tenant_id: str, api_options: APIOptions, user_context: Dict[str, Any]):
# send custom response like this
api_options.response.set_status_code(200) # or any other status code
json_dict = {'message': 'Custom response'}
api_options.response.set_json_content(json_dict)
# this return doesn't matter. But we must do it
# cause the function signature expects a response.
return EmailExistsGetOkResult(False)
original_implementation.email_exists_get = email_exists_get
return original_implementation
passwordless.init(
override=passwordless.InputOverrideConfig(
apis=override_passwordless_apis
)
)