In the previous part, we covered how to prepare the best environment to develop apps on Azure in the easiest way. Now we will look forward to developing our API app for Azure and test it locally. Afterward, we will secure our API with a specific key which will be stored in Azure Key Vault. Also, our user data will be stored in Azure Cosmosrtaş

DEVELOPING AND TESTING OUR API LOCALLY
I find an Instagram API which provides us some information such as user id, biography, and profile picture URL about an Instagram user.

Now let’s move on to our code.
In the previous, we had created a Post function. Let’s use it for our Post Method. But first, we should create a CosmosDB to store our data.
On the home page, search for CosmosDB.

Create a CosmosDB.

Choose your database. I will use MongoDB for this blog. It does not matter what you choose.

Decide your resource type.
Request Unit database account: In Azure Cosmos DB, Request Units (RU) is a measure of throughput. They reflect the resources (CPU, memory, and I/O) necessary to conduct a single read or write operation on a database item of a specific size. The RU/s (Request Units per Second) statistic for Cosmos DB is used to assess the performance level required to satisfy the needs of your application. Azure Cosmos DB is a worldwide distributed NoSQL database service that supports a variety of API models, including the Core (SQL) API, MongoDB API, Cassandra API, Gremlin API, and Table API.
vCore Cluster: vCore Cluster is a fully managed relational database service. It offers numerous performance levels to accommodate varying workload requirements. In compared to the DTU (Database Transaction Units) based paradigm, the vCore-based purchasing strategy provides more flexibility and control over the resources given to the database. You can choose the number of vCores and the amount of memory you allot to your database in the vCore model, giving you greater granular control over performance and cost.
Because we won’t create heavy queries, Request Unit looks good to me.

Create your resource.

After being deployed, go to the resource, and select Data Explorer.

Create New Database.

I have reached the limit I set because I have another database. But you won’t have any problems.

In the database, there are 2 collections that I have. One of for User data, other one is for my credentials such as tenant id, client id, and secret key for authentication. I store this information in CosmosDB because we will need them for Key Vault connection, and I did not want to store these credentials in a .json file or etc.
Respectively,
ID 1 is for Tenant ID,
ID 2 is for Client ID and
ID 3 is for Secret ID.
You can create and obtain the secret key from an app registration. Later, we will integrate this app into our key vault object and make it accessible with our secret key.
Click New Document and add these values like this.

In Connection Strings Menu, you can find the Primary Connection String of your database. We will use it in our code to create a connection with the database.

We’re ready with CosmosDB. The last thing that we need is Key Vault.
Go home page and search for Key Vault.

Go to create.

Create Key Vault.

Go to the resource and generate a new secret from Secrets menu.

After you generate a secret, go to Access Policies, and create an access policy.

Give permissions. Get and List are enough for this scenario.

In the principal menu, choose the app registration that you created before for secret key.

And we are done with all integrations! The only thing left now is to develop our application via VS Code.
Here is my own code for post actions.
username = req.body && req.body.username;
key = req.body && req.body.key;
These variables obtain body object that we use in Postman. The parameter “username” will get information about an Instagram user that match this username.
Key variable is our secret key that we store in Key Vault.
If key object does not match in secret key stored in Key Vault, we will get Bad Request 400.
This body will get credentials to obtain our Secret Key from Key Vault.
const secretCollection = client.collection("secrets");
const get_tenant = await secretCollection.findOne({ id: "1" });
const tenant_id = get_tenant.value;
const get_client = await secretCollection.findOne({ id: "2" });
const client_id = get_client.value;
const get_secret = await secretCollection.findOne({ id: "3" });
const secret_id = get_secret.value;
const keyVaultUri = `your keyvault url`;
const credential = new ClientSecretCredential(
tenant_id,
client_id,
secret_id
);
const secretClient = new SecretClient(keyVaultUri, credential);

I assigned our Secret Key to variable mySecret.
mySecret.value is the value of our Secret Key.
If our body key object match with Secret Key, the post action will end successfully.
const secretClient = new SecretClient(keyVaultUri, credential);
const mySecret = await secretClient.getSecret("for-blog");
if (username && key && key === mySecret.value) {
const collection = client.collection("instagram");

In the function.json file, you can limit the type of actions. You can list the methods by which this function can be used in this list. in this way, you will provide convenience in the error handling section.

Let’s try our code with Postman!
Run and debug the code from Visual Studio Code

And our code is running. You can see the running functions and function URL’s.

Launch Postman and try your API.
Paste the URL and give body parameters.

Send the request.
We received 200 OK and response message! Let’s check the database on Azure.
And finally, we successfully posted data to Azure CosmosDB!

