Create
Last updated
Last updated
The create event allows you to add new records to your data models.
In this tutorial we will cover:
Setting up a POST endpoint
Linking our POST endpoint to an action
Adding a create event
Generate our code & test
For the purpose of this example, we will use a data model that holds different user sign up information.
Our goal is to set up a POST Endpoint that allows our users to create a new record in this data model whenever the endpoint is triggered.
There are two ways to do this, we can either:
Use the API Generator, see API Generator, OR
Build the endpoint, action & event from scratch.
In this article we will cover how to build the endpoint, action & event from scratch.
In this section we are going to create a POST Endpoint manually to create a new record inside our Users table. To do this, head on over to the endpoints tab & press the "+ New Endpoint" button to create a new endpoint.
Next, we fill in the basic details for our endpoint as below.
Example configuration below for our Create a new user endpoint.
Notice, we set the Request Method to Post.
Next, we need to configure the "Variables" section of our POST Endpoint. You might be asking yourself why do we need a variable in our endpoint?
The reason that we need a variable is that our endpoint is going to create a new user record. The user will need a username, email & password, however we don't know what this information will be in advance. This means that we need to create some placeholders inside our endpoint that we can send data to.
Note, the placeholders can receive ANY data of a particular Kind e.g. text, number, number with decimal. Because the placeholders can receive ANY data of a particular Kind we call these placeholders, "variables".
If you can imagine entering a username, email & password at random & then sending this to the variable slots inside our endpoint, this might look like:
In the example above, we are sending the following values to the variable slots inside our endpoint.
username : James
email : james@dittofi.com
password : test123
To set up our endpoint to receive variable data, Dittofi allows you to send data to an endpoint as body variables, header variables, path variables & query variables. Each of these different variable types has a slightly different implementation. In this case we are going to use a body variable, which is the simplest. You can add a body variable to your endpoint as shown below.
Notice, the configuration of the body variable is of Kind Model & the model that we link the variable to is our Users Sign Up Data model.
By linking the model to the Users Sign Up Data model, in the background we are creating placeholders for the values inside our User Sign Up Data Model.
The Label & Name should be set to something meaningful, so that we will remember what is contained inside of the body variable.
Lastly, we press "Save" & close our endpoint.
Next, we need to link our endpoint to an action. To do this, we start by heading over to the actions tab & press "+ New Action" & give our action a name. The best idea here is to give the action the same name as your endpoint, since it will make it easier to search for in the future.
Next, under the "Advanced" section we link our action to our Endpoint "Create a new user", as shown below.
With this configuration, this means that every time the "Create a new user" endpoint is hit, this will start our action.
By itself our action is not doing anything currently, but we can add events to our action & events graph on the right hand side of the screen by pressing the plus button.
You're able to chain together events to accomplish various things that you might want your action to do, for example, Create, Read, Update or Delete records in your data models, but also to upload files, distribute notifications over websockets, loop through data sets & so on.
In our case, we want to "Create" a new record, so this means selecting the Kind Create from the dropdown.
Each kind has a different set of options that need to be configured. In the case of the "Create" event the event can be configured as below.
Notice, we set the following fields:
Lastly, we press save & close our event.
The final step is to generate our code & test it. To generate the code, press the "Generate code" button.
Next, to test our entire workflow, we step over to the endpoints tab, open up our endpoint & press the "Run" button.
Notice, that when we run the endpoint, a popup box appears in the center of the screen. The popup box includes options to input the user data that we will send to our endpoint. This is shown below.
Notice that the Email, Username & Password fields here are just placeholders that we can enter free text into. This information will then be sent to our POST Endpoints body variable, which will then be forwarded on to our Action & Event that will write the data into our User Sign Up Data model.
For example, entering in the following dummy values creates the following record in our User Sign Up Data model.
Option
Description
Example
Name
The name you give to your endpoint. The name you give should correspond to what you're trying to accomplish with the end point.
Create a new user
Path
The path of your endpoint is the URL extension that your endpoint will live under.
/v1/create_new_user
Description
The description field describes what our endpoint will be used for.
A new endpoint to create a new user in our users table
Request method
The request method describes what the endpoint will be doing, the options are GET, POST, PUT & DELETE.
POST (this is used whenever we want our endpoint to create new data inside our data model)
Res. content type
The response that your endpoint is expecting from your action.
application / JSON
Req. content type
The request content that your endpoint is expecting when run.
application / JSON
Auth. Profile
If you want to lock down your endpoint to only run for authenticated users.
Please select a profile (this means that there is no authentication profile added).
Event options
Description
Example
Type
This option creates a variable inside our event, we select the type model since we are expecting to take new user data from our endpoint & that data is of type model.
model
Model
The model that you want to store data inside your event. In our case
User sign up data
Assign
Here we map data from our endpoint into the model variable inside our event.
username = UserSignUpData.Username
As
This is a variable that your event will return to your action. The "As" variable can be used later in your event chain.
Created User