How to retrieve SqlDatabase pricing using the Azure Pricing API

bonda29 60 Reputation points
2024-09-23T12:11:16.28+00:00

I have an instance of SqlDatabase in Java that contains the following details:

{
	"sqlServerName": "bonda",
	"sqlServerLocation": "germanywestcentral",
	"isPatchUpdate": false,
	"name": "test-db-1",
	"innerObject": {
		"sku": {
			"name": "GP_S_Gen5",
			"tier": "GeneralPurpose",
			"size": null,
			"family": "Gen5",
			"capacity": 1
		},
		"kind": "v12.0,user,vcore,serverless,freelimit",
		"innerProperties": {
			"maxSizeBytes": 34359738368,
			"status": "Paused",
			"currentServiceObjectiveName": "GP_S_Gen5_1",
			"defaultSecondaryLocation": "germanynorth",
			"zoneRedundant": false,
			"maxLogSizeBytes": 193273528320,
			"readScale": "Disabled",
			"currentBackupStorageRedundancy": "Local",
			"minCapacity": 0.5,
			"isLedgerOn": false,
		},
		"location": "germanywestcentral",
		"name": "test-db-1",
		"type": "Microsoft.Sql/servers/databases"
	}
}

How can I use the Azure Pricing API (https://prices.azure.com/api/retail/prices) to get pricing information for a it?

Azure SQL Database
{count} votes

Accepted answer
  1. Sina Salam 10,566 Reputation points
    2024-09-23T15:57:10.3766667+00:00

    Hello bonda29,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    I understand that you would like to use the Azure Pricing API (https://prices.azure.com/api/retail/prices) to get pricing information with an instance of your SqlDatabase in Java

    You will need to construct an API request that will use the Azure Pricing API endpoint you identified https://prices.azure.com/api/retail/prices and apply filters to narrow down the specific SQL Database configuration requirement like Service Name, SKU Name, and Region. For an example:

     GET https://prices.azure.com/api/retail/prices?$filter=serviceName eq 'Azure SQL Database' and skuName eq 'GP_S_Gen5' and armRegionName eq 'germanywestcentral
    

    The below is an example of how your code should be in Java.

       import java.io.BufferedReader;
       import java.io.InputStreamReader;
       import java.net.HttpURLConnection;
       import java.net.URL;
       public class AzurePricingAPI {
           public static void main(String[] args) {
               try {
                   String url = "https://prices.azure.com/api/retail/prices?$filter=serviceName eq 'Azure SQL Database' and skuName eq 'GP_S_Gen5' and armRegionName eq 'germanywestcentral'";
                   HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
                   conn.setRequestMethod("GET");
                   BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                   String inputLine;
                   StringBuffer content = new StringBuffer();
                   while ((inputLine = in.readLine()) != null) {
                       content.append(inputLine);
                   }
                   in.close();
                   conn.disconnect();
                   System.out.println(content.toString());
               } catch (Exception e) {
                   e.printStackTrace();
               }
           }
       }
    

    I hope this is helpful! Do not hesitate to let me know if you have any other questions.

    Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.