The REST API is a JSON web service that facilitates communication between Mattermost clients, as well as integrations, and the server. The server is currently on API version 4.
Looking for the API reference? You can find it here: https://api.mattermost.com.
To add an endpoint to API version 4, all of the following must be completed:
At Mattermost, the OpenAPI specification is used for API documentation. The API documentation lives in the main Mattermost repository alongside the server: api.
To document an endpoint, follow these steps:
Find the .yaml
file in the api/v4/source directory that fits your endpoint.
GET /users/{user_id}
endpoint you would be looking for the users.yaml file.Copy an existing endpoint from the same or a different file.
Update the documentation you copied with the correct information for your endpoint, including:
Tag
- the resource typeSummary
- a summary of few wordsDescription
- a brief 1-2 sentence descriptionPermissions
- the permission(s) requiredParameters
- the URL and body parametersResponses
- the success and error responsesConfirm you don’t have any syntax errors by running make build
within the api directory.
Continue with the implementation of your API handler, updating this documentation as needed.
To implement the API handler, you’ll first need to setup your developer environment, and then follow these steps:
Add the declaration for your endpoint. For an example, check out the /api4/user.go file.
Implement the handler for your endpoint. Follow this general pattern for handlers:
func handlerName(c *Context, w http.ResponseWriter, r *http.Request) {
// 1. Parse the request URL and body.
// 2. Do a permissions check if required.
// 3. Invoke handler logic through the app package.
// 4. (Optional) Check the Etag.
// 5. Format the response and write the response.
}
For examples, see the createUser() and the getUser() handlers.
Run the server by runing make run-server
within the server directory.
Use curl
or Postman to test the basics of your endpoint.
The Go driver for APIv4 is in /model/client4.go. To add a function to support your new endpoint:
Copy over an existing driver function, such as CreateUser.
Paste the function into the section for your endpoint. For example, POST /teams
would go in the Teams section.
Modify the function to correctly hit your endpoint. Make sure to update the request method to match your endpoint’s HTTP method.
The most important part of this process is to make sure the new endpoint works correctly. Follow these steps to write a unit test:
Open the test Go file related to your endpoint, or create one if necessary. For example, if you put your handler in /api4/user.go, your test will go in /api4/user_test.go.
Write your test based on the other tests in your file (or folder). There are several helper functions in /api4/apitestlib.go that you may use.
Ensure that your test covers the following:
Returning the correct error code might require investigation in the app or store to find the source of errors. Status codes on errors should be set at the creation of the error.
Submit your pull request against the mattermost/mattermost repository by following these instructions.
The Mattermost API used to be defined in https://github.com/mattermost/mattermost-api-reference, but the source has since been moved to the mattermost/mattermost to streamline making code and documentation changes at the same time.