Home
%3CLINGO-SUB%20id%3D%22lingo-sub-1056637%22%20slang%3D%22en-US%22%3EBuilding%20REST%20API%20with%20Python%2C%20Flask%20and%20Azure%20SQL%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1056637%22%20slang%3D%22en-US%22%3E%3CP%3EI%20mentioned%20in%20my%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2FAzure-SQL-Database%2F10-Reasons-why-Azure-SQL-is-the-Best-Database-for-Developers%2Fba-p%2F969055%22%20target%3D%22_self%22%3Eprevious%20article%3C%2FA%3E%20that%20having%20native%20JSON%20support%20in%20Azure%20SQL%20it%E2%80%99s%20a%20game%20changer%20as%20it%20profoundly%20change%20the%20way%20a%20developer%20can%20interact%20with%20a%20relational%20database%2C%20bringing%20the%20simplicity%20and%20the%20flexibility%20needed%20in%20today%E2%80%99s%20Modern%20Applications.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAs%20Python%20is%20becoming%20immensely%20popular%2C%20one%20of%20the%20most%20common%20tasks%20for%20a%20developer%20is%20to%20create%20REST%20API%20using%20Python.%20Thanks%20to%20JSON%20support%2C%20using%20Azure%20SQL%20as%20a%20backend%20database%20to%20support%20your%20API%20is%20as%20easy%20as%20writing%20to%20a%20text%20file%2C%20with%20the%20difference%20that%20behind%20the%20scenes%20you%20have%20all%20the%20peace%20of%20mind%20that%20your%20data%20will%20be%20safely%20stored%20and%20made%20available%20on%20request%2C%20at%20scale%2C%20with%20also%20the%20option%20to%20push%20as%20much%20compute%20to%20data%20as%20you%20want%2C%20so%20that%20you%20can%20leverage%20the%20powerful%20query%20and%20processing%20engine%20while%20keeping%20your%20code%20simple%2C%20elegant%20and%20agile%2C%20with%20a%20clear%20separation%20of%20concerns.%20All%20these%20things%20will%20help%20you%20immensely%20once%20you%E2%80%99ll%20start%20to%20evolve%20your%20project%20to%20keep%20it%20updated%20with%20today%E2%80%99s%20demanding%20and%20ever-changing%20world.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20see%20by%20yourself%20how%20simple%20it%20is%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20center%3B%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F160793i16B388D15404B36A%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22clipboard_image_0.png%22%20title%3D%22clipboard_image_0.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EA%20dictionary%20is%20created%20with%20the%20CustomerID%20property.%20The%20dictionary%20is%20then%20passed%20to%20the%20stored%20procedure%20as%20a%20JSON%20document%2C%20which%20will%20return%20a%20JSON%20document%20as%20a%20result.%20The%20stored%20procedure%20is%20really%20simple%20as%20well%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20center%3B%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F160795i59EA471FD2F8EA0D%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22clipboard_image_2.png%22%20title%3D%22clipboard_image_2.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFirst%20line%20shows%20how%20JSON_VALUE%20help%20to%20extract%20data%20from%20JSON%20when%20you%20are%20looking%20for%20a%20scalar%20value.%20Then%20you%20get%20the%20column%20you%20need%20from%20the%20Customers%20table%2C%20also%20specifying%20the%20shape%20you%20want%20the%20resulting%20JSON%20to%20have%20by%20aliasing%20the%20column%26nbsp%3B%20with%20a%20name%20using%20the%20common%20dot%20notation%20to%20represent%20relationships%20and%20hierarchies.%20Then%20you%20use%20FOR%20JSON%20to%20simply%20tell%20Azure%20SQL%20that%20the%20result%20must%20be%20a%20JSON%20document%2C%20not%20a%20tabular%20resultset.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20center%3B%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F160797i43AC3A9AF904EB4A%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22clipboard_image_4.png%22%20title%3D%22clipboard_image_4.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThat%E2%80%99s%20it.%20Minimum%20effort%20yet%20amazing%20value.%20Clear%20separation%20of%20concerns%2C%20abstraction%20from%20database%20schema%20via%20stored%20procedure%20usage%2C%20efficient%20query%20performance%20via%20index%20usage%20(and%20you%20don%E2%80%99t%20have%20to%20do%20anything%20special%20to%20maintain%20the%20index%20updated%2C%20it%20will%20be%20done%20automatically%20by%20Azure%20SQL%20behind%20the%20scenes)%2C%20database%20and%20application%20working%20perfectly%20together%20as%20a%20team%2C%20thanks%20the%20JSON%20being%20the%20glue%20that%20keep%20the%20solution%20together%2C%20and%20also%20favor%20loose%20coupling%20and%20thus%20scalability.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAnd%20while%20you%2C%20as%20a%20developer%2C%20are%20working%20to%20create%20a%20nice%20UI%20for%20the%20new%20API%20just%20created%2C%20someone%20else%20can%20query%2C%20process%20and%20update%20the%20data%2C%20even%20using%20very%20powerful%20and%20resource%20consuming%20processes%20without%20affecting%20your%20performance%20%3CEM%3Eat%20all%2C%20%3C%2FEM%3Eas%20isolation%20can%20be%20guaranteed%20using%20read-only%20copies%20and%20geographical%20distributed%20database.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENot%20bad%20for%20just%20a%20bunch%20of%20lines%20of%20code%2C%20right%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EEven%20better%2C%20the%20whole%20sample%20is%20available%20here%20for%20you%20to%20play%20with%2C%20and%2C%20especially%20if%20you%20are%20a%20new%20developer%2C%20it%20will%20also%20have%20some%20interesting%20link%20to%20videos%20and%20tutorials%20on%20how%20to%20use%20Python%2C%20Flask%20and%20Visual%20Studio%20Code%20to%20create%20the%20API%20I%20just%20described.%20Enjoy!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure-Samples%2Fazure-sql-db-python-rest-api%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2FAzure-Samples%2Fazure-sql-db-python-rest-api%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1056637%22%20slang%3D%22en-US%22%3E%3CP%3ECreating%20REST%20API%20with%20Azure%20SQL%20is%20never%20been%20easier%20thanks%20to%20its%20native%20JSON%20support.%20Let's%20see%20how%20we%20can%20use%20Python%20and%20Flask%20to%20create%20an%20elegant%20and%20modern%20solution%20to%20serve%20REST%20API%20to%20the%20world%3C%2FP%3E%3C%2FLINGO-TEASER%3E
Microsoft

I mentioned in my previous article that having native JSON support in Azure SQL it’s a game changer as it profoundly change the way a developer can interact with a relational database, bringing the simplicity and the flexibility needed in today’s Modern Applications.

 

As Python is becoming immensely popular, one of the most common tasks for a developer is to create REST API using Python. Thanks to JSON support, using Azure SQL as a backend database to support your API is as easy as writing to a text file, with the difference that behind the scenes you have all the peace of mind that your data will be safely stored and made available on request, at scale, with also the option to push as much compute to data as you want, so that you can leverage the powerful query and processing engine while keeping your code simple, elegant and agile, with a clear separation of concerns. All these things will help you immensely once you’ll start to evolve your project to keep it updated with today’s demanding and ever-changing world.

 

You can see by yourself how simple it is:

 

clipboard_image_0.png

 

A dictionary is created with the CustomerID property. The dictionary is then passed to the stored procedure as a JSON document, which will return a JSON document as a result. The stored procedure is really simple as well:

 

clipboard_image_2.png

 

First line shows how JSON_VALUE help to extract data from JSON when you are looking for a scalar value. Then you get the column you need from the Customers table, also specifying the shape you want the resulting JSON to have by aliasing the column  with a name using the common dot notation to represent relationships and hierarchies. Then you use FOR JSON to simply tell Azure SQL that the result must be a JSON document, not a tabular resultset.

 

clipboard_image_4.png

 

That’s it. Minimum effort yet amazing value. Clear separation of concerns, abstraction from database schema via stored procedure usage, efficient query performance via index usage (and you don’t have to do anything special to maintain the index updated, it will be done automatically by Azure SQL behind the scenes), database and application working perfectly together as a team, thanks the JSON being the glue that keep the solution together, and also favor loose coupling and thus scalability.

 

And while you, as a developer, are working to create a nice UI for the new API just created, someone else can query, process and update the data, even using very powerful and resource consuming processes without affecting your performance at all, as isolation can be guaranteed using read-only copies and geographical distributed database.

 

Not bad for just a bunch of lines of code, right?

 

Even better, the whole sample is available here for you to play with, and, especially if you are a new developer, it will also have some interesting link to videos and tutorials on how to use Python, Flask and Visual Studio Code to create the API I just described. Enjoy!

 

https://github.com/Azure-Samples/azure-sql-db-python-rest-api