🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
Rest API
This page was created by Hans.karlsen on 2023-05-21. Last edited by BSMaintenance on 2026-06-08.
The parameter provided for 'container' is invalid.

Exposing MDriven Turnkey as REST Services

MDriven Turnkey allows you to expose your applications as REST services, enabling external systems to interact with your MDriven data. This tutorial guides you through setting up a ViewModel to act as a data source for REST requests, using MDrivenRest/get and MDrivenRest/post to expose and update data, and executing operations via REST.

Setting Up a ViewModel for REST

To expose a ViewModel as a REST service, configure it to act as a data source:

  1. Open your ViewModel in MDriven Designer.
  2. Select the head of the ViewModel and set the property RestAllowed to true.
    #: This setting allows the ViewModel to be accessed via REST requests.
  3. Upload the updated model to your MDriven Turnkey site.

Once configured, your ViewModel is ready to be accessed using REST.

Using MDrivenRest/get

The MDrivenRest/get endpoint allows you to retrieve data from your ViewModel. To access data:

  1. Construct the URL using the format: /MDrivenRest/get?ViewModelName=YourViewModelName&RootObjectId=YourRootObjectId.
    #: Replace YourViewModelName with the name of your ViewModel and YourRootObjectId with the ID of the root object.
  2. Access the URL to retrieve data in a REST format.

This method returns the data as a REST document, which can be consumed by external systems.

Using MDrivenRest/post

To update data via REST, use the MDrivenRest/post endpoint. Here's how:

  1. Create a new ViewModel action to handle the POST request.
  2. Define a nesting in your ViewModel to hold the data you want to update.
  3. Use the selfVM.RestPost operator in an action to send data.
    The RestPost operator requires the target URL, user, password, and nesting name as parameters.

For example, to update a registration number, you can:

selfVM.RestPost('http://yourdomain.com/MDrivenRest/post?ViewModelName=EditOneCar&RootObjectId=' + self.ExternalId, '', '', 'DataToPost')

This command posts the data in the DataToPost nesting to the specified ViewModel.

Executing Operations via REST

REST services in MDriven can also execute operations defined in your ViewModel. Actions available at the root level of a ViewModel will be executed during both GET and POST requests.

  1. Define actions in your ViewModel that modify data or perform operations.
  2. Use parameters in your REST URL to pass additional data to these actions.

For instance, you can append parameters to your URL like &SomeParam=Value to influence the behavior of the action.

Example: Updating Data via REST Post

Consider a scenario where you want to update a car's registration number:

  1. Set up a ViewModel with a variable vNewRegNum to hold the new registration number.
  2. Create a generic column in your ViewModel to display this variable.
  3. Define an action using selfVM.RestPost to send the updated registration number back to your system.

This approach allows you to update data in your MDriven Turnkey application using REST.

By following these steps, you can effectively expose your MDriven Turnkey applications as REST services, enabling external systems to interact with your data efficiently. For more on consuming REST services, see Documentation:Rest Services In MDriven.

Source

Based on the MDriven video MDriven Turnkey | REST services in MDriven.