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 ThirdPartyEmailPassword from "supertokens-node/recipe/thirdpartyemailpassword";
ThirdPartyEmailPassword.init({
override: {
apis: (originalImplementation) => {
return {
...originalImplementation,
emailPasswordEmailExistsGET: 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/emailpassword/epmodels"
"github.com/supertokens/supertokens-golang/recipe/thirdpartyemailpassword"
"github.com/supertokens/supertokens-golang/recipe/thirdpartyemailpassword/tpepmodels"
"github.com/supertokens/supertokens-golang/supertokens"
)
func main() {
thirdpartyemailpassword.Init(&tpepmodels.TypeInput{
Override: &tpepmodels.OverrideStruct{
APIs: func(originalImplementation tpepmodels.APIInterface) tpepmodels.APIInterface {
(*originalImplementation.EmailPasswordEmailExistsGET) = func(email string, options epmodels.APIOptions, userContext supertokens.UserContext) (epmodels.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 epmodels.EmailExistsGETResponse{
OK: &struct{ Exists bool }{
Exists: false,
},
}, nil
}
return originalImplementation
},
},
})
}
from supertokens_python.recipe import thirdpartyemailpassword
from supertokens_python.recipe.emailpassword.interfaces import APIOptions, EmailExistsGetOkResult
from typing import Dict, Any
from supertokens_python.recipe.thirdpartyemailpassword.interfaces import APIInterface
def override_thirdpartyemailpassword_apis(original_implementation: APIInterface):
async def email_exists_get(email: 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.emailpassword_email_exists_get = email_exists_get
return original_implementation
thirdpartyemailpassword.init(
override=thirdpartyemailpassword.InputOverrideConfig(
apis=override_thirdpartyemailpassword_apis
)
)