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? That can be found here: https://api.mattermost.com.
To add an endpoint to API version 4, each item on the following checklist must be completed:
A full example can be found through these two pull requests:
POST /teams endpoint: mattermost-api-reference#72POST /teams endpoint: mattermost-server#5220At Mattermost we use the OpenAPI specification for API documentation. That documentation lives in the mattermost-api-reference repository. To document an endpoint, follow these steps:
Fork mattermost-api-reference and create a branch for your changes.
Find the .yaml file in the /source/v4 directory that fits your endpoint.
GET /users/{user_id} endpoint you would be looking for users.yamlCopy an existing endpoint from the same or a different file.
Update the documention you copied with the correct information for your endpoint, including:
Confirm you don’t have any syntax errors by running make build-v4 and copying /html/static/mattermost-openapi-v4.yaml into the Swagger editor.
Commit your changes and submit a pull request to mattermost-api-reference.
If you’re looking for examples, see users.yaml.
To implement the API handler you’ll first need to setup your developer environment, then follow these steps:
Add the declaration for your endpoint.
Implement the handler for your endpoint.
The general pattern for handlers is
func handlerName(c *Context, w http.ResponseWriter, r *\ http.Request) {
// 1. Parsing of request URL and body
// 2. Permissions check if required
// 3. Invoke logic through the app package
// 4. (Optional) Check the Etag
// 5. Format the response and write the response
}
For examples, see the updateUser() and the getUser() handlers.
Run the server using make run-server to check for syntax errors.
(Optional) Use curl or Postman to test the basics of your endpoint. The endpoint will also be tested through a unit test, so this step is optional.
The Go driver for APIv4 is in /model/client4.go.
To add a function to support your new endpoint:
Copy an existing driver function, such as CreateUser.
Paste the function into the section for your endpoint.
POST /teams would go in the Teams sectionModify the function to correctly hit your endpoint.
That’s it, you’ll be able to test your function in the next section.
The most important part of this process is to make sure your endpoint works correctly. Follow these steps to write a test:
Open the test Go file related to your endpoint.
Write your test based on the other tests in your file
Make sure your test covers the following:
Returning the correct error code might require investigation in the app or store packages to find the source of errors. Status codes on errors should be set at the creation of the error.
When completing this step, please make sure to use the new model.NewAppError() function (see example).
Please submit a pull request against the mattermost/mattermost-server repository by following these instructions.