---
updatedAt: 2025-07-15T08:41:29.000Z
agentTools:
  projectIndex: https://docs.appery.io/llms.txt
---

# Creating APIs for Relational Database (Ionic App)

Exposing a relational database via REST API in an Ionic Appery.io app

# Introduction

The purpose of this tutorial is to show you how fast it is to expose a relational database via REST APIs and integrate it into an Ionic mobile app.

This tutorial uses [HostBuddy](https://www.hostbuddy.com/) hosting and a [MySQL](https://hosting.review/web-hosting-glossary/#24) database. You can use any other relational database with **[API Express](https://docs.appery.io/docs/apiexpress-overview)** from [Appery.io](https://appery.io/).

> 📘 Custom SQL Query
>
> If you would like to write a custom SQL query and expose it via a REST API, use the **[SQL](https://docs.appery.io/docs/apiexpress-visual-service-builder-sql-component)** component to do that.

# Creating Database

**[API Express](https://docs.appery.io/docs/apiexpress-overview)** can connect to any SQL database. In this tutorial, we will use an SQL database from [HostBuddy](https://www.hostbuddy.com/).\
Alternatively, you can set up a database as a [Postgres Database On Heroku](https://docs.appery.io/docs/apiexpress-databaseconnection-heroku-postgres).

To set up your database, follow these steps:

1. Go to the [HostBuddy website](http://www.hostbuddy.com/) and start a free trial. A credit card is not required for the trial, so just enter your credentials, such as login name, email, and password, and click **Sign Up** to continue.
2. You will shortly receive an email from HostBuddy, so click on the activation link provided there.
3. Sign in to the [HostBuddy website](http://www.hostbuddy.com/) and click **Try Now** to start using the free trial. No need to change anything, just click **Next**, **Submit**, then wait while your hosting is prepared (you will be notified by an email that your hosting account is activated).
4. Once it’s ready, click the corresponding button to log in to your hosting control panel:

<Image align="center" className="border" border={true} src="https://files.readme.io/c35b631-2023-03-27_15-28-23.png" />

5. Now, click **DATABASES** to open the *Database Manager* and then click the **+Add Database** button:

<Image align="center" className="border" border={true} src="https://files.readme.io/c1b817f-2023-03-27_15-29-22.png" />

6. Type in the database name and password, and confirm.\
   Your database is created and ready to be filled up with data: ***DB name***, ***MYSQL Server Name***, and ***Login ID*** with ***Password*** (you will need those in the step described in the **[Creating Database Connection](https://docs.appery.io/docs/apiexpress-quickstart-creating-apis-for-relational-database#creating-database-connection)** section):

<Image align="center" className="border" border={true} src="https://files.readme.io/edd4c4a-2023-03-27_15-30-55.png" />

## Creating Database Table

In this step, you are going to create a database table.

1. For the database you have just created, click the **Actions** button on the right and select **phpMyAdmin** from the drop-down:

<Image align="center" className="border" width="smart" border={true} src="https://files.readme.io/563a46f-Screenshot_15.png" />

2. Once the *phpMyAdmin* dashboard opens, input the server, username, and password. Click **Go**.
3. To create a new table, go to the **SQL** tab in the top menu bar, select the database on the left, and paste the code below:

```sql Create table
CREATE TABLE users(
    id INT PRIMARY KEY NOT NULL,
    name VARCHAR(32)
);
```

4. Then click **Go**:

<Image align="center" className="border" border={true} src="https://files.readme.io/732ebe6-Screenshot_16.png" />

A database with one table will be created.

5. Select the newly created table **users** on the left (under your database) and click **SQL** in the top toolbar.
6. Replace the default code in the SQL editor with the following SQL query to insert three users and click the **Go** button.

```sql Insert users
INSERT INTO users(id, name) VALUES (1, 'Alex'), (2, 'Bred'), (3, 'Colin');
```

You should now see that 3 rows have been inserted: the three created **users** records can be seen when you select the ***users*** collection:

<Image align="center" className="border" border={true} src="https://files.readme.io/105845b-2023-03-27_15-35-39.png" />

Now, that the database is ready, we can proceed with creating a database connection for API Express.

> 📘 How to Add API Express into Mobile Application
>
> You might also like this video for detailed instructions on how to:
>
> * create a **[Database connection](https://docs.appery.io/docs/apiexpress-database-connection)** to a relational DB,
> * expose the relational DB data as a REST service and
> * use this functionality in an [Appery.io](https://appery.io) Ionic mobile app:

<HTMLBlock>{`
<iframe width="610" height="315" src="https://www.youtube.com/embed/LaLpEb5r-fU" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
`}</HTMLBlock>

# Exposing Database via REST APIs

There are two general steps in working with this database and exposing it via REST APIs in API Express:

1. Creating a database connection.
2. Automatically generating REST APIs for a database table.

Let's start with the first step: creating a database connection.

# Creating Database Connection

To create a new [database connection](https://docs.appery.io/docs/apiexpress-database-connection), go to the **[API Express](https://appery.io/apiexpress)** tab, click **Create new DB connection**, and provide a name for a new connection, for example, ***mysqlDB***:

<Image align="center" className="border" border={true} src="https://files.readme.io/2fd6aef-2023-03-27_15-36-37.png" />

Now, you can proceed with providing your external relational database data.

> 📘 Note!
>
> For [HostBuddy](https://www.hostbuddy.com/), your DB credentials can be found under **MySQL Manager**.

For example:

| Parameter name           | Parameter value                                                                                                                                                      |
| :----------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Database connection name | mysqlDB                                                                                                                                                              |
| Connection type          | Relational database                                                                                                                                                  |
| Database type            | MySQL                                                                                                                                                                |
| Host                     | `mysqlxxxx.site4now.net`. This value can be found under **DATABASES > Database Manager > MySQL Manager**, as **MySQL 5.x Server Name** of the needed database table. |
| Port                     | 3306                                                                                                                                                                 |
| Database name            | `db_xxxxx_mysqldb`. This value can be found **DATABASES > Database Manager > MySQL Manager** of the needed database table.                                           |
| Username                 | xxxxxx\_mysqldb. This value can be found under **DATABASES >Database Manager > MySQL Manager**, as **Login ID** of the needed database table.                        |
| Password                 | The password you typed in when creating the corresponding MySQL database.                                                                                            |

After the required credentials are selected/entered, click the **Save** button to save the connection:

<Image align="center" className="border" border={true} src="https://files.readme.io/2b56389-2023-03-27_15-40-49.png" />

> 📘
>
> Check [this](https://docs.appery.io/docs/apiexpress-quickstart-creating-apis-for-relational-database#creating-database) section to find the *Host*, *Database name*, and *Username* credentials.

## Testing Connection

Once you are done entering all the connection values you can test the connection to ensure it works correctly. Simply click the **Test** button. You should see the *Test successful* message:

<Image align="center" className="border" width="smart" border={true} src="https://files.readme.io/5c77802-2023-03-27_15-40-09.png" />

# Creating API Express Project with APIs

Now that the database connection is created, you need to create an [API Express project](https://docs.appery.io/docs/apiexpress-project) that will hold the actual APIs.

1. From the **Connection** tab, open the connection list and select **Back to list**:

<Image align="center" className="border" border={true} src="https://files.readme.io/46313d0-Screenshot_31.png" />

2. In the **Projects** section, click **Create new project**.
3. Enter a project name: ***mysqlProject*** and optionally enter a description. Then click **Create**:

<Image align="center" className="border" border={true} src="https://files.readme.io/39ccfc1-2023-03-27_15-42-36.png" />

4. From the **API** tab, click the project **new service** link:

<Image align="center" className="border" border={true} src="https://files.readme.io/928e3ac-Screenshot_32.png" />

5. For **Service group name**, enter ***users***. Keep **Generate REST API** checked.
6. Click the **Create** button:

<Image align="center" className="border" width="smart" border={true} src="https://files.readme.io/ca0e66d-Screenshot_36.png" />

7. On the next page, for **Database connection**, select the connection you just created (*mysqlDB*).
8. For **Table**, select the **users** table.
9. For **Actions**, keep all the default values.
10. Click the **Generate service** button to save and generate the APIs:

<Image align="center" className="border" border={true} src="https://files.readme.io/22ce0a8-Screenshot_37.png" />

When the APIs are generated you should see the following page:

<Image title="Screenshot_38.png" alt={1489} align="center" border={true} src="https://files.readme.io/cbfb7f2-Screenshot_38.png">
  REST APIs.
</Image>

Congratulations, you have just exposed a relational database via REST APIs!

## Testing APIs

To [test](https://docs.appery.io/docs/apiexpress-testing) any API, click the **test** link on the right:

<Image align="center" className="border" border={true} src="https://files.readme.io/50d0a5d-Screenshot_39.png" />

On the **Service test** page, you can test any of the API generated:

<Image title="Screenshot_40.png" alt={612} align="center" border={true} src="https://files.readme.io/6794da4-Screenshot_40.png">
  Service test - GET
</Image>

# Building Ionic App

Now that you have successfully exposed your database via REST APIs (created the app backend), the next step is to create a mobile app that will use these APIs.

In this section, we will demonstrate how to create a simple Ionic Mobile app that uses the API you just created.

## Creating New App

1. Go to the **[Apps](https://appery.io/app/projects)** page.
2. Click the **Create new app** button.
3. Enter *IonicDatabaseApp* for the app name.
4. Select the **[Ionic Blank](https://docs.appery.io/docs/ionic-5-app-templates#ionic-5-blank)** template and click **Create**.

In a moment, you will find yourself inside Appery.io **[App Builder](https://docs.appery.io/docs/apperyio-app-builder-overview)**.

## Designing App UI

The app UI will be very simple and consist of just the **[List](https://docs.appery.io/docs/ionic-5-ui-components-properties#list)** component.

1. When the **[App Builder](https://appery.io/appbuilder)** loads the new app, open **Pages > Screen1**.
2. Go to the page **[Header](https://docs.appery.io/docs/2-creating-an-app-ui-pages-and-tabs#header-and-footer)** and under the **[PROPERTIES](https://docs.appery.io/docs/1-creating-an-app-ui-palette#properties-overview)** view, set the **[Toolbar Title](https://docs.appery.io/docs/11-creating-an-app-ui-ionic-5-ui-components-properties#toolbar-title)** component's **Text** property as ***Ionic Database App***. You can also do it by typing in right in the mobile frame:

<Image align="center" className="border" border={true} src="https://files.readme.io/2ae0212-2023-03-27_15-16-46.png" />

3. Drag the **[List](https://docs.appery.io/docs/ionic-5-ui-components-properties#list)** UI component and drop it to the **[Content](https://docs.appery.io/docs/2-creating-an-app-ui-pages-and-tabs#content-component)** area.\
   A list with two default **[List item](https://docs.appery.io/docs/ionic-5-ui-components-properties#list-item)** each having a child **[Item Label](https://docs.appery.io/docs/ionic-5-ui-components-properties#item-label)** component will be created.
4. [Delete](https://docs.appery.io/docs/1-creating-an-app-ui-palette#context-menu-overview) the second **[List item](https://docs.appery.io/docs/ionic-5-ui-components-properties#list-item)** since we will be loading the data from the API:

<Image align="center" className="border" border={true} src="https://files.readme.io/0d2efa1-2023-03-27_15-18-54.png" />

4. You can now save the UI by clicking the **SAVE** button in the **[App Builder Toolbar](https://docs.appery.io/docs/apperyio-app-builder-overview#toolbar)**.

## Importing API Services

To work with the services, we will need to import the API services.

1. In the **[App Builder](https://appery.io/appbuilder)**, select **CREATE NEW > API Express Generator**.
2. Select the API Express project you have just created (here, ***mysqlProject***).
3. Click the **generate** link on the right, then the **Generate** button (keep the *Service Name* default).
4. After generating the service, click the **Refresh** button.

<Image align="center" className="border" width="smart" border={true} src="https://files.readme.io/fd2997f-2023-03-27_15-59-44.png" />

All the generated services should now be listed under the **Services > API Express > Models > users** folder:

<Image align="center" className="border" width="smart" border={true} src="https://files.readme.io/273a6bf-2023-03-27_16-45-44.png" />

## Defining Service

To get the list of database records, we will also need to create a datasource and it's time that the created service be added to the page.

1. So, go back to the **Screen1** page and open its **[DATA](https://docs.appery.io/docs/apperyio-app-builder-overview#data-panel)** panel to add the needed datasource:
2. Select ***users\_find1*** from the drop-down for a  new datasource and click the **Add** button to add this service. You can rename it to what is needed.
3. Now, click the **Success Mapping** button:

<Image align="center" className="border" border={true} src="https://files.readme.io/8fe1839-2023-03-27_16-47-27.png" />

4. In the *Mapping editor*, click **Expand all** for both **Service response** and **Page** panels, recreate the below service mapping, and then click **Save & Replace**:

<Image align="center" className="border" border={true} src="https://files.readme.io/59fec92-2023-03-27_16-48-27.png" />

## Invoking Service

The final step is invoking the datasource we have just added.

1. Switch to the page **[DESIGN](http://google.com)** panel and expand the **[EVENTS](https://docs.appery.io/docs/apperyio-app-builder-overview#events-tab)** tab from the bottom.
2. For the **COMPONENT**, select ***Page***.
3. For the **EVENT**, set ***After page showing***.
4. For the **ACTION**, set ***[Invoke service](https://docs.appery.io/docs/5-event-handling#invoke-service)***, select your service (here, **users\_find1**), and click the **Save** button:

<Image align="center" className="border" border={true} src="https://files.readme.io/dbd3b40-2023-03-27_16-49-15.png" />

# Testing App

You can now save your work and click the **TEST** button in the **[App Builder Toolbar](https://docs.appery.io/docs/apperyio-app-builder-overview#toolbar)** to run the app in the preview.\
The app will launch in the browser and you should see the users from your relative database:

<Image align="center" className="border" width="smart" border={true} src="https://files.readme.io/a997992-2023-03-27_16-50-39.png" />

> 📘 Want to Know More?
>
> To learn how to expose a relational database via REST APIs and use it a jQuery Mobile application, check [this](https://docs.appery.io/docs/apiexpress-quickstart-creating-apis-for-relational-database) document or watch the video tutorial about [How to Build an App with API Express and External REST API for JQM framework](https://youtu.be/F9PRXU1VeaE?t=200).