Blog Post

Educator Developer Blog
6 MIN READ

A Comprehensive Guide to Getting Started with Data API Builder for Azure SQL Database or SQL Server

kevin_comba's avatar
kevin_comba
Iron Contributor
Jul 08, 2023

Getting started with Data API builder for Azure SQL Database or SQL Server

 

 

What is Data API builder?

Data API builder for Azure Databases provides modern REST and GraphQL endpoints to your Azure Databases. Database objects can be safely exposed via REST or GraphQL endpoints with Data API builder, allowing your data to be accessed using modern approaches on any platform, language, or device. Granular security is ensured by an integrated and configurable policy engine; integration with Azure SQL,

SQL Server, PostgreSQL, MySQL, and Cosmos DB provides developers with unprecedented productivity.

Data API Builder Features

  1. REST operations like CRUD ( POST, GET, PUT, PATCH, DELETE) , filtering, sorting and pagination.
  2. GraphQL operations like queries and mutation, queries and mutations.
  3. Support authentication via OAuth2/JWT.
  4. Role-based authorization using received claims.
  5. Allow collections, tables, views and stored procedures to be accessed via REST and GraphQL.
  6. Full integration with Static Web Apps via Database Connection feature when running in Azure.
  7. Easy development via dedicated CLI.
  8. Open Source, you can always contribute

Our Focus.

In this blog, I’m going to walk you through a simple process to get started with Data API Builder with Microsoft SQL Database deployed on Azure. This can also be done using Azure Cosmos Db, Azure Database for PostgreSQL and Azure MySQL Database.

Prerequisites.

  • Basic understanding of Rest CRUD operations like POST and GET
  • Basic understanding of a relational database like Microsoft SQL or MYSQL
  • Willingness to follow along and learn as the above two prerequisites are not a must.

Requirements to get started.

Please click the below links to download .Net 6 and Vs Code editor, you can search Live Preview and Thunder client in the Vs code Extension Tab and install them.

Procedure

  1. Provision and deploy Microsoft SQL on Azure
  2. Create a Data API builder.
  3. Test our API with Thunder client & Insomnia
  4. Link our Azure SQL DB via Data API Builder to our simple HTML Page to view our Data.

 

Provision and deploy Microsoft SQL on Azure

 

 

 

  • Search for SQL databases

 

 

  • Click create to start provisioning your service.

 

 

  • Select your subscription, create a resource group, enter a database name and select elastic pool no for this is just a demo project.

 

 

  • Create your SQL server and select a location.

 

 

  • For authentication I have selected both SQL and Azure Active directory, but SQL alone is enough for authenticating this project. Add sever admin and password and keep the safe because we will need them later.

 

 

 

  • After creating the SQL server, select locally – redundant backup for demo purposes.

 

 

  • For these options, let’s go with the defaults.

 

 

 

  • For these options, let’s go with the defaults.

 

 

  • Create tags to identify your services on your resource group and create Review + create

 

 

 

  • Click go to resources after deployment is complete

 

 

 

  • On the overview you will see all the essential details of our SQL Database and SQL Server.

 

 

 

 

  • Click on the Query editor and sign in with either SQL Server auth or Azure Active Directory

 

 

  • You can view your database name below

 

 

 

  • Click new query and add this code below to create two tables (Brand & Category) then click run to execute these queries.

 

 

 

 

  • Add this insert statements to insert data in our database.

 

 

 

  • click run to execute and check the message below ‘query succeeded’.

 

 

 

  • refresh to view the newly created tables.

 

 

  • You can easily view tables available in our database

 

 

 

  • Run the below SQL Statement to confirm data inside our table.

 

 

 

 

  • On the connection string copy ADO.NET(SQL authentication) we will use it late.(NB: do not expose your connection string with password, I will do so for demo purposes)

 

 

Create a Data API builder.

 

  • Back to our windows open windows PowerShell and type dotnet, if the below message doesn’t appear. Install .NET 6.0

 

  • Run the below command to install Data API Builder locally.

 

 

  • Confirm installation was successful by running the below command.

 

 

Test our API with Thunder client & Insomnia

 

 

  • Open any folder you want to work on with VS Code then open your terminal. Run dab init –database-type “mssql” –connection-string “enter-your-connection-string-we-copied-in-the-above-steps”. Its good practice to use .env file on your root folder and call it on our connection string ie dab init --database-type “mssql” --connection-string "@env('my-connection-string')"

 

 

  • The command generates a config file called dab-config.json looking like this:

 

 

  • Data-source will directly enable us to specify the database we are using and connection-string.The runtime will specify that our Data API will be consumed by both Rest endpoint is made available at the path `/api/<entity>` and GraphQL endpoint is available at `/graphql`. Under host we specify the mode of our data API either production or development, cors enables us to set origins example if yourdormain.com is your client app which will be sending request to your API you should add it under origins. Under authentication you can specify how you want to be authenticated.

 

 

 

  • Add the below code for the brand category entities. This entities maps directly to your tables on Azure SQL database, wo do this by specifying the source. Permissions lets use specify the actions, “*” means all operations Create Edit Read Delete are enabled. With role you can assign people roles to do this actions above, anonymous will let anyone do all the actions.

 

 

  • To start run the code below.

 

 

 

 

  • launch your Thunder client VS code extension( if on windows use CTRL + SHIFT + R). create a new request, select GET and https://localhost:5001/api/category. Click send and you should get a response from our deployed Azure SQL database.

 

 

 

 

 

  • The GET verb also supports several query parameters (also case sensitive) that allow you to manipulate and refine the requested data:
    • $orderby: return items in the specified order
    • $first: the top n items to return
    • $filter: expression to filter the returned items
    • $select: list of field names to be returned

 

 

 

  • The query parameters select will Only select CategoryID field after you click send.

 

 

  • The query parameters `filter` and `eq` will return CategoryName equal to Category x after you click send.

 

 

  • Data API offers validations, example you can’t just send categoryName as the only field because our table requires both categoryName and categoryID. An error will be returned.

 

 

 

  • REST Endpoint POST: create a new category after you add the json object below with that data and  click send.

 

 

  • REST Endpoint POST: create a new category after you add the json object below with that data and  click send.

 

 

  • REST Endpoint PUT: will update a category after you add the json object below with that data and  click send.

 

 

 

  • Whenever you need to access a single item, you can get the item you want through a GET request by specifying its primary key example CategoryID

 

 

  • To delete a record by specifying its primary key example CategoryID and set the HTPP request to delete, and click send.

 

 

  • GraphQL endpoint is available at `/graphql`, set the HTPP request to post and provide the schema below and click send. For this example we need REST client like Postman or Insomnia to query. I have used Insomnia.

 

 

 

 

  • This is how we filter using the filter method.

 

  • This is how we orderBy descending or ascending.

 

 

 

 

Link our Azure SQL DB via Data API Builder to our simple HTML Page to view our Data.

 For this demo am using a simple html page with jQuery and bootstrap. is helping me to make the HTTP request to our API server using fetch, get the response and append it to our html page dynamically.

  • Step one – add this code in index.html. and right click on the code to open with live server. Copy the URL displayed after the html page loads.

 

 

  • Step two – Paste the URL below on the dab-config.json on origins. Save your code. On your terminal `CTRL + C` to terminate the  Data API server and start it again using `dab start`.

 

  • There you go it, successfully connected your static web app with Data API builder for Azure Databases engine.

 

 

Read more:

 

 

Updated Jul 06, 2023
Version 1.0
  • Faisal1775's avatar
    Faisal1775
    Brass Contributor

    Getting started with Data API Builder for Azure SQL Database or SQL Server involves several steps. Here's a comprehensive guide to help you through the process:

    1. Prerequisites:

      • An Azure subscription with access to Azure SQL Database or an on-premises SQL Server.
      • An understanding of SQL queries and database concepts.
    2. Set up Azure SQL Database or SQL Server:

      • If you haven't already, create an Azure SQL Database in the Azure portal or set up a SQL Server on-premises.
      • Ensure that you have the necessary credentials (server name, database name, username, password) to connect to the database.
    3. Install and configure Data API Builder:

      • Data API Builder is a component of Azure API Management. If you don't have an API Management instance, create one in the Azure portal.
      • Once your API Management instance is set up, navigate to it and go to the "APIs" section.
      • Click on the "Add API" button and select "Blank API" to create a new API.
      • Provide the necessary details such as the API name and URL suffix.
      • In the "Design" tab, click on the "+ Add operation" button to define operations for your API.
      • Specify the HTTP method (GET, POST, PUT, DELETE) and the URL template for each operation.
      • Under "Request Processing," select "Send to Backend" and configure the backend settings to connect to your Azure SQL Database or SQL Server.
      • Provide the necessary connection details, including the server name, database name, and authentication credentials.
      • Save your API configuration.
    4. Test and secure your API:

      • Once your API is configured, you can test it by sending requests using a tool like Postman or through the Azure portal's built-in testing interface.
      • Ensure that the API is functioning as expected, retrieving data from the database or making updates.
      • Consider implementing security measures such as authentication and authorization to control access to your API. Azure API Management provides various security options like OAuth, API keys, or Azure Active Directory integration.
    5. Publish and manage your API:

      • After testing and securing your API, it's time to publish it to make it accessible to consumers.
      • In the Azure portal, navigate to your API Management instance and go to the "APIs" section.
      • Select your API and click on the "Publish" button to make it available for consumption.
      • Consider setting up API policies to define additional behaviors, such as rate limiting or response transformations.
    6. Document and monitor your API:

      • Document your API by providing clear and comprehensive documentation for consumers to understand how to interact with it.
      • Azure API Management provides built-in tools for generating and publishing API documentation.
      • Set up monitoring and analytics to track API usage, performance, and errors. Azure API Management offers various monitoring capabilities, including logs, metrics, and alerts.

    Remember to regularly update and maintain your API as your application evolves and new requirements arise.

    Please note that the steps provided here are general guidelines, and the specific configuration may vary depending on your environment and requirements. Consult the official Microsoft Azure documentation for detailed instructions and the latest information on using Data API Builder with Azure SQL Database or SQL Server.

  • I have DAB up and running on an on-premises test database where the SQL server is remote from the DAB server. Everything works well and I can query stored procedures via Insomnia, but only via "https://localhost:5001" I cannot work out how to change this so that it will work via the IP address, so that it can be quired remotely...

  • arunsurfer's avatar
    arunsurfer
    Copper Contributor

    Useful content and detail explanation, may I know what tool is used in the design diagram