## 4.1.6.1.3 Set Up Your Application for Multitenancy
Learn how to add multitenancy to your application and make it available for other subaccounts using the SaaS Provisioning service and the SAP Authorization and Trust Management service.
## Context
You've created an application in your subaccount that is secured by the SAP Authorization and Trust Management service. You now want to make that application available to other subaccounts (tenants). You'll use the SaaS Provisioning service to make your application available to a consumer subaccount within your global trial account.
You declare your application security descriptor with JSON syntax and store it in a flat file on the filesystem. The standard name for this file is xs-security.json .
## Procedure
1. Enable multitenancy in the application security descriptor file.
- a. Go to the folder where the xs-security.json file is stored.
- b. Change the value of the tenant-mode parameter to shared .
- c. Under the scopes element, add access to the SaaS Provisioning service to call the product list callback API directly. You'll implement the callbacks in Step 3.
<!-- image -->
```
}, { "name": "$XSAPPNAME.Callback", "description": "With this scope set, the callbacks for tenant onboarding, offboarding and getDependencies can be called.", "grant-as-authority-to-apps": [ "$XSAPPNAME(application,sap-provisioning,tenantonboarding)" ] } ],
```
- d. Save the file.
2. Update the manifest.
In this step, you need to complete the following tasks:
- Add a new routing pattern.
- Add the service binding for the SaaS Provisioning service.
- a. Go to your application folder and open the manifest.yml file.
- b. For the application router, add the TENANT\_HOST\_PATTERN parameter under the env parameter.
This parameter specifies a generic route for all tenants to call the application over the approuter.
```
 Sample Code env: destinations: > [ {"name":"hw-dest", "url":"https://product-list-ap25.cfapps.eu10.hana.ondemand.com", "forwardAuthToken": true} ] TENANT_HOST_PATTERN: "^(.*)-approuter-product-listap25.cfapps.eu10.hana.ondemand.com"
```
<!-- image -->
##  Restriction
The value of the TENANT\_HOST\_PATTERN parameter must be in lowercase.
- c. Add the service binding for the SaaS Provisioning service to your application.
Adding the service binding of the SaaS Provisioning service in the manifest.yml file will automatically bind the service instance to your application, when deploying it.
<!-- image -->
3. Implement the subscribe/unsubscribe endpoints.
To enable other subaccounts to subscribe to your application, you need to implement an endpoint for the SaaS registration manager to subscribe/unsubscribe.
- a. Go to the myapp folder and open the index.js file.
- b. Add the following lines of code after the checkReadScope function. (Replace the ap25 string with the string that you used when deploying your application security model. Adapt the region code if your trial isn't in the eu10 region.)
```
 Sample Code app.put('/callback/v1.0/tenants/*', function (req, res) { var consumerSubdomain = req.body.subscribedSubdomain; var tenantAppURL = "https:\/\/" + consumerSubdomain + "-approuterproduct-list-ap25." + "cfapps.eu10.hana.ondemand.com/products"; res.status(200).send(tenantAppURL); }); app.delete('/callback/v1.0/tenants/*', function (req, res) { // Implement here any offboarding proces that's triggered when user deletes a subscription to the application res.sendStatus(200); });
```
Return the HTTP 200 OK success status response if the offboarding process was successful.
If the offboarding failed, return the relevant error code and an accompanying message.
##  Note
It's highly recommended to use a response object that contains both the error code and a userfriendly, customer-oriented error message and technical details about the error in the following format:
```
{ "code":<The relevant response code, e.g., 500, 400>, "messageForCustomer":"<A customer-facing message. Please write in a user-friendly format, as clearly as possible>", "errorDetails":"<Specify here additional technical details about the error>" }
```
- c. To be able to read the body of those calls, add the body parser module at line 9 of the index.js file.
## 
```
Sample Code const bodyParser = require('body-parser') app.use(bodyParser.json())
```
- d. Add the body parser module as a dependency to the product list/myapp/package.json file.
4. Create a SaaS configuration file.
```
 Sample Code "dependencies": { "express": "^4.17.1", "@sap/xsenv": "^2.2.0", "@sap/xssec": "^3.0.0", "passport": "^0.4.1", "body-parser": "^1.19.0" }
```
To make your multitenant application endpoints available for subscription to consumer subaccounts, you need to register the application in the Cloud Foundry environment by using the SaaS Provisioning service.
To register your application, you need a configuration file called config.json . In this file, you specify the subscription URL, the name, and description of your application. The xsappname must be the same as the xsappname in the xs-security.json file.
- a. Go to your application folder and create the config.json file.
- b. Insert the following lines.
5. Delete the old service instance of the SAP Authorization and Trust Management service.
```
 Sample Code { "xsappname":"product-list", "appUrls": { "onSubscription" : "https://product-listap25.cfapps.eu10.hana.ondemand.com/callback/v1.0/tenants/{tenantId}" }, "displayName" : "Product List MTA", "description" : "Product list MTA sample application", "category" : "Custom SaaS Applications" }
```
When you change the tenant mode from dedicated to shared like you did in step 1, it's not enough to update the service instance. You have to first unbind and delete the old service instance, to be able to later recreate it with the updated tenant mode settings.
- a. Unbind the existing service instance from your application.
2. b.
3. c.
6. Create service instances and redeploy your application.
- a. Log in to your Cloud Foundry account with the Cloud Foundry CLI.
```
cf unbind-service <APP_NAME> <SERVICE_INSTANCE>
```
```
 Sample Code cf unbind-service product-list xsuaa-service-tutorial
```
```
Unbind the existing service instance from the application router. cf unbind-service <APP_NAME> <SERVICE_INSTANCE>  Sample Code cf unbind-service approuter xsuaa-service-tutorial Delete the existing service instance.
```
```
cf delete-service <SERVICE_INSTANCE>  Sample Code cf delete-service xsuaa-service-tutorial
```
- b. Go to your application folder and create the service instance with the xs-security.json security descriptor file.
- c. Create the SaaS Provisioning service instance with the config.json file.
- d. Redeploy the application with the updated manifest.yml file.
7. Create a route for a consumer subaccount.
<!-- image -->
<!-- image -->
<!-- image -->
Make your application reachable for consumer subaccounts by adding a new route in the Cloud Foundry CLI. The route is composed of the subdomain of the subscribing subaccount and the TENANT\_HOST\_PATTERN of the application router that you defined in the manifest.yml file. Y ou have to create a new route for every subaccount (tenant) that subscribes to the application.
- a. Log in to the Cloud Foundry account where the application is deployed with the Cloud Foundry CLI.
- b. Create a route for the consumer subaccount.
8. Access the application with the consumer subaccount.
<!-- image -->
To access the application you need to subscribe to it. Follow these steps to subscribe to the SaaS application with the consumer subaccount and call the application URL.
- a. Open the SAP BTP trial.
- b. Navigate to your consumer subaccount.
- c. Choose Subscriptions .
- d. Choose Product List MTA .
- e. Choose Subscribe .
- f. Choose Go to Application .
You'll now see the application with the message no data because you have to assign the role collection to your user in the consumer subaccount.
9. Assign the role collection.
Assign the ProductListViewer role collection to your user. This role collection contains the necessary role to view the products in your application.
- a. Open the SAP BTP cockpit.
- b. Navigate to your consumer subaccount.
- c. Choose Security Role Collections .
- d. Choose the ProductListViewer role collection.
- e. Use the chevron at the right side to expand the role collection.
- f. Go to the Users section and choose Edit .
- g. Enter the e-mail address of the user that you want to assign to the role collection. Take care that your user's identity provider is SAP ID service.
- h. Save your changes.
- i. Clear your cache and reload the application URL.
## 
```
Sample Code https://consumer-tenant-ap25-approuter-product-listap25.cfapps.eu10.hana.ondemand.com/products
```
The application will now show you the products.
## Related Information
Add Multitenancy to a Node.js Application Secured by the SAP Authorization and Trust Management service