This guide will walk you through installing the framework and building your first REST API in MATLAB.
If you prefer an interactive Live Script format, see toolbox/doc/GettingStarted.m.
You have two primary ways to install matlab-http-server:
- Download the latest
.mltbxfile from the Releases page. - Double-click the file in MATLAB's Current Folder browser.
- Follow the installation prompts. This adds the framework to your MATLAB path permanently.
- Clone the repository:
git clone https://github.com/PVDecker1/matlab-http-server.git - Add the
toolboxfolder to your path:addpath('C:\path\to\matlab-http-server\toolbox')
- MATLAB R2022b or later: The framework relies heavily on the
dictionarytype introduced in R2022b. - No Toolboxes Required: Core functionality works with base MATLAB.
- Parallel Computing Toolbox (Optional): Required if you want to use
parfevalfor non-blocking asynchronous handlers. - Go Binary (Optional): Required only when you explicitly select the Go transport.
In matlab-http-server, you define API endpoints by subclassing mhs.ApiController. Create a file named HelloController.m:
classdef HelloController < mhs.ApiController
methods (Access = protected)
function registerRoutes(obj)
% Map URL paths to handler methods
obj.get('/hello', @obj.sayHello);
obj.post('/echo', @obj.echoData);
end
end
methods
function res = sayHello(~, ~, res)
% res = sayHello(obj, req, res)
% res is the response object you modify and return
res.json(struct('message', 'Hello, MATLAB!'));
end
function res = echoData(~, req, res)
% Access the request body via req.Body
res.json(req.Body);
end
end
endOnce your controller is defined, start the server and register an instance of it. Create a script named startServer.m:
% Create the server on port 8080
server = MatlabHttpServer(8080);
% Register your controller
server.register(HelloController());
% Start listening for connections
server.start();
disp('Server is running at http://localhost:8080');Run this script in MATLAB. You should see a message confirming the server has started.
Use curl or any API client (like Postman or Insomnia) to test your endpoints.
curl http://localhost:8080/helloResponse: {"message":"Hello, MATLAB!"}
PowerShell / Linux / macOS:
curl http://localhost:8080/echo -d '{"name":"Austin"}' -H "Content-Type: application/json"Windows CMD (Requires escaped quotes):
curl http://localhost:8080/echo -d "{\"name\":\"Austin\"}" -H "Content-Type: application/json"Response: {"name":"Austin"}
The framework includes several examples to demonstrate more advanced features:
- BasicExample: (
toolbox/examples/BasicExample/) Shows basic routing, POST handling, and path parameters. - MultiControllerExample: (
toolbox/examples/MultiControllerExample/) Demonstrates how to split a large API into multiple controller classes. - StaticSiteExample: (
toolbox/examples/StaticSiteExample/) Shows how to serve a frontend (HTML/CSS) alongside your API. - SignalAnalyzer: (
toolbox/examples/SignalAnalyzer/) A more complex example featuring a React frontend served from the same MATLAB HTTP server. If the preferred example port is busy, the script automatically chooses a nearby free port.
To run an example, navigate to its folder in MATLAB and run the run... script.
- Controller Testing With
mhs.ApiTestCase: Test controllers through a real router without opening live sockets. - Routing Guide: Learn about path parameters, specific route ordering, and supported HTTP verbs.
- Request & Response Reference: Detailed documentation of the
HttpRequestandHttpResponseclasses. - Static File Serving: How to host your frontend assets directly from the server.