Documentation › JSON API › Helper functions
Your generated codebase will contain a cmd/api.helpers.go file which contains miscellaneous helper functions for the code in the cmd/api package.
The exact contents of this file will vary depending on the options you select, but it may include the following functions:
| Function | Description |
|---|---|
newEmailData() |
Returns a map containing common data for use in email templates. |
newAuthenticationToken() |
Generates and signs a new JWT authentication token for the specified user ID and returns the token along with its expiry time. |
backgroundTask() |
Runs a function in a background goroutine, automatically recovering from panics and reporting any returned errors. Used for running background tasks. |
You should feel free to create any additional helper functions that you need in this file. If your helper needs access to a dependency – such as the database or logger – you should implement the helper as a method on the application struct.
func (app *application) exampleHelper() {
// Helper code goes here...
}