Creating a new API endpoint
All endpoints are defined inside ./src/app.py
. To create a new endpoint, simply create a new function and add a new route to the api
object.
-
Navigate to
./src/app.py
. -
Define a new route (name and methods) through the following syntax:
@api.route("/test", methods=[GET,POST]) def test(): return "Hello World!"
The above code will create an endpoint called
test
such that when accessed will show"Hello World!"
.
NOTE
The function name can be anything, but it is recommended to keep it consistent with the endpoint name.
-
Define logic for endpoint.
- Inside the function body, you can access the query string provided through the
request.args.get("<query name>")
- Handle errors, do computations, etc...
Flask offers many different ways to return data. For more information, see the Flask documentation.
You can:
- return a string literal
- return a json object
- render a template (html file)
- redirect to a different route or url
- return a file
- etc...
- Inside the function body, you can access the query string provided through the