Create a CAP Application with SAP HANA Cloud from Scratch and Deploy to Cloud Foundry in SAP BTP
Share

[[{“value”:”

Create a CAP Application with SAP HANA Cloud from Scratch and Deploy to Cloud Foundry

This guide walks you through the complete lifecycle of building a CAP (Cloud Application Programming) application, connecting it to SAP HANA Cloud, and deploying it to SAP BTP Cloud Foundry.

 

1.Create SAP BTP Trial Account

You need a trial account to access Cloud Foundry, SAP HANA Cloud, and all BAS development tools.

 

2. Subscribe to SAP Business Application Studio (BAS)

If not subscribed:

  1. Go to SAP BTP Cockpit → Instances and Subscriptions
  2. Subscribe to Business Application Studio

 

3. Create CAP Project Skeleton (Using Template Wizard)

Open BAS and press:

Ctrl + Shift + P → Template Wizard

MaheshSirsat1708_0-1763211188026.png

 

Then follow these steps:

3.1 Select Template

  • Select: CAP Project Generator
  • Click Next

MaheshSirsat1708_1-1763211188035.png

 

3.2 Provide Project Details

  • Project Name → Any name of your choice
  • Choose Database → SQLite (default)
  • Choose Deployment Target → Cloud Foundry

Note:
Do not select SAP HANA Cloud here.
We first deploy the application with SQLite, then add HANA later from terminal.

MaheshSirsat1708_2-1763211188038.png

 

3.3 Choose Runtime Capabilities

Select these productive services:

  • XSUAA
  • SAP BTP Connectivity Service
  • SAP BTP Destination Service
  • SAP BTP Application Router

Click Finish.

MaheshSirsat1708_3-1763211188042.png

This creates the complete folder structure including:

  • /db
  • /srv
  • /app
  • mta.yaml
  • Security and deployment artifacts

MaheshSirsat1708_4-1763211188044.png

 

 

4. Modify MTA Configuration

Cloud Foundry sometimes fails when resource names contain dynamic interpolation (org/space).

Removing them ensures deployment simplicity.

Open mta.yaml and remove:

-${org}-${space}

from all resource names.

MaheshSirsat1708_5-1763211188053.png

 

5. Create a Sample CAP Model

5.1 Define Database Schema

Open /db/schema.cds:

MaheshSirsat1708_6-1763211188054.png 

5.2 Create Service Definition

Open /srv/service.cds:

MaheshSirsat1708_7-1763211188057.png

Now, you have a simple end-to-end data + service model to validate local and cloud deployment.

 

6. Test the CAP Application Locally

 

cds w

Now, CAP runs using SQLite, allowing quick testing without any external services.

MaheshSirsat1708_8-1763211188059.png

 

MaheshSirsat1708_9-1763211188063.png

 

 7. Deploy the CAP Application (SQLite Mode)

  1. Right-click mta.yaml → Build MTA Project
    • Or use: 

mbt build

        2. Login to Cloud Foundry
               Ctrl + Shift + P → CF Login

MaheshSirsat1708_10-1763211188066.png

         3. Deploy the generated .mtar

    • Right-click .mtar → Deploy MTA Archive
    • Or use:
    • cf deploy yourfile.mtar

MaheshSirsat1708_11-1763211188068.png

Your CAP app runs in Cloud Foundry with SQLite.

MaheshSirsat1708_12-1763211188071.png

 

MaheshSirsat1708_13-1763211188078.png

Next, we will connect it to real SAP HANA Cloud

 

8. Create SAP HANA Cloud Instance

Go to:

SAP BTP Cockpit → Instances & Subscriptions → Create

Choose:

  • Service: SAP HANA Cloud
  • Plan: hana
  • Environment: Cloud Foundry
  • Instance Name: hanaDB

Set the DB administrator password → required for Data Explorer.

 

MaheshSirsat1708_37-1763212044767.png

And press on Create instance.

MaheshSirsat1708_38-1763212044776.png

It will take estimated 10-15min

It creates the actual HANA database where CAP HDI containers will be deployed.

 

9. Subscribe to SAP HANA Cloud Tools

Without this subscription, you cannot access the DB instance UI.

Go to:
Instances & Subscriptions → Subscribe → SAP HANA Cloud Tools

MaheshSirsat1708_39-1763212044789.png

Assign Roles

Under Users, assign all required HANA Cloud roles.

MaheshSirsat1708_40-1763212044792.png

Sign out → Sign in again.

 

10. Open SAP HANA Data Explorer

Click the HANA instance → Open SAP HANA Database Explorer

MaheshSirsat1708_41-1763212044799.png

 

MaheshSirsat1708_42-1763212044805.png

First login:

  • Username: DBADMIN (or the username shown under “Administrator”)
  • Password: the one you entered during instance creation

MaheshSirsat1708_43-1763212044807.png

If you forgot your DB username

Open:
SAP HANA Cloud Administration → Overview
Your administrator username will be displayed there.

MaheshSirsat1708_44-1763212044810.png 

Now, You have direct access to schemas, HDI containers, and database objects.

 

11. Open HDI Container Section in SAP HANA Database Explorer

Inside SAP HANA Database Explorer:

  1. Click the “+” icon
  2. Choose Cloud Foundry
  3. Log in to your CF environment

MaheshSirsat1708_45-1763212044812.png

 

  1. From the list → choose Instance Type: HDI Container

MaheshSirsat1708_46-1763212044815.png

At this stage, your CAP HDI container will NOT appear yet, because we have not created it.

 

12. Add HANA Support to CAP Application

Run the following commands in BAS terminal:

cds add hana
npm i

What these commands do

  • cds add hana
  • Generates HANA-specific configuration
  • Creates HDI deployment artifacts
  • Prepares the project to run on SAP HANA instead of SQLite
  • npm i
    • Installs additional dependencies required for HANA runtime

    MaheshSirsat1708_47-1763212044816.png

    Now, your project is ready to connect and deploy models to an HDI container.

     

    13. Update package.json for HANA Deployment

     Open package.json → under “cds.requires” add:

    “db”: {
    “kind”: “hana”
    }

    MaheshSirsat1708_48-1763212044818.png

    This tells CAP to use SAP HANA as the primary database in production.

     

    14. Bind the CAP Project to Your HANA Instance

    In BAS → Cloud Foundry view:

    1. Select your CAP app
    2. Choose Bind to Service
    3. Pick your HANA DB instance hanaDB

    This automatically creates a .env file containing:

    • HDI container name
    • Credentials
    • Database URLs
    • Service keys

    MaheshSirsat1708_49-1763212044820.png

    Why we are doing this

    CAP needs this information to connect to SAP HANA for:

    • Local “cds watch”
    • Production deployment

    Your project is now fully connected to SAP HANA Cloud.

     

    15. Build Your CAP Project for Production

    Run:

    cds build –production

    MaheshSirsat1708_50-1763212044821.png

    HANA Cloud can now deploy your database schema properly.

     

    16. Deploy to SAP HANA Cloud

    Run:

    cds deploy –production

    MaheshSirsat1708_51-1763212044822.png

    This:

    • Creates HDI container
    • Deploys CDS schema into HANA tables
    • Activates views, indexes, constraints

    Your CAP application now uses SAP HANA Cloud as its primary DB.

     

    17. Rebuild and Deploy the Entire CAP Application

    Again perform:

    mbt build

    Then deploy:

    cf deploy <yourfile>.mtar

    MaheshSirsat1708_52-1763212044825.png

     

    MaheshSirsat1708_53-1763212044828.png

    Our complete application (service + database) is deployed on Cloud Foundry using HANA.

     

    18. Verify Schema Deployment in SAP HANA Cloud

    1. Open SAP HANA Data Explorer
    2. Click +
    3. Select Instance Type = HDI
    4. Choose your CAP application HDI container    

    MaheshSirsat1708_54-1763212044831.png

     

            5. Expand Tables and Views

    MaheshSirsat1708_55-1763212044834.png

    You can verify that:

    • Tables are created
    • Data model is deployed correctly

     

    19. Running CAP Application Locally Using HANA Cloud

    If you want to run locally via HANA:

    1. Ensure .env exists
    2. Rebind HDI container if needed

    MaheshSirsat1708_56-1763212044838.png

     

    MaheshSirsat1708_57-1763212044841.png

     

    1. Run:     

    cds w

    Successful connection check

    If logs show:

    Connected to SAP HANA Cloud (hanaDB) . Then your HDI container is successfully connected.

    MaheshSirsat1708_58-1763212044855.png

     

    20. Validate Deployment Using Postman

    MaheshSirsat1708_59-1763212044858.png

     

    This confirms:

    • Service is running
    • HANA DB is connected
    • Deployment is successful end-to-end

    Conclusion

    In this blog, we have completed the following activities:

    • Created a new CAP application using the BAS template wizard.

    • Performed an initial deployment using SQLite to verify application behavior.

    • Provisioned a SAP HANA Cloud instance and assigned all required role collections.

    • Enabled HANA support in the CAP project and generated HDI-related artifacts.

    • Bound the project to the HANA instance to establish secure database connectivity.

    • Built CDS artifacts for production and deployed them into the HDI container.

    • Rebuilt and deployed the full MTA application to SAP BTP Cloud Foundry using HANA as the primary database.

    • Verified successful deployment through SAP HANA Database Explorer and API testing.

    “}]] 

      Read More Technology Blog Posts by Members articles 

    #abap

    By ali