How to Utilize SQL-APIConsumer for API Integration in SQL Server

May 7, 2023 | Programming

Welcome to the SQL-APIConsumer project! This powerful tool is designed for seamless API consumption via SQL Server, specifically built using C#. Whether you’re a developer looking to enhance data accessibility or a data analyst seeking quick insights, this guide will help you get started with SQL-APIConsumer.

Getting Started

The heart of this project lies in the main procedure: APICaller_Web_Extended. It allows you to call various web methods, returning important data about the API response, including headers and status codes. If you need a straightforward result without extra details, you can opt for APICaller_WebMethod.

Using APICaller_Web_Extended

This procedure accepts parameters for HTTP methods, URLs, headers, and JSON bodies. Here’s how the parameters work:

  • httpMethod: The HTTP method to call (e.g., GET, POST).
  • URL: The intended API endpoint.
  • Headers: Request headers if needed.
  • JsonBody: JSON body for requests needing it.

Upon execution, you will receive an integer indicating success (0) or failure (-1), along with comprehensive API response details.

Sample Code

Let’s say we want to retrieve data using the GET method. The process can be visualized as following a recipe:

  • Prepare your ingredients: HTTP method, URL, and headers (like gathering flour and sugar for a cake).
  • Call the procedure which processes these ingredients (like mixing them to make the cake batter).
  • Receive the result, which includes delicious data as if you are pulling a perfectly baked cake out of the oven!

DECLARE @httpMethod nvarchar(max) = 'GET'
DECLARE @URL nvarchar(max) = 'https://www.routingnumbers.info/api/name.json?rn=122242597'
DECLARE @Headers nvarchar(max) = '[Name: Content-Type, Value: text/javascript; charset=utf-8]'
DECLARE @JsonBody nvarchar(max) = ''

DECLARE @ts AS TABLE(Json_Result nvarchar(max), ContentType varchar(100), ServerName varchar(100), 
                      StatusCode varchar(100), Description varchar(100), Json_Headers nvarchar(max))

DECLARE @i AS INT  
INSERT INTO @ts
EXECUTE @i = [dbo].[APICaller_Web_Extended] @httpMethod, @URL, @Headers, @JsonBody

SELECT * FROM @ts

Troubleshooting Tips

If you encounter any issues, keep these troubleshooting ideas in mind:

  • Ensure your SQL Server version is 2016 or later for full compatibility.
  • If you get a “Could not obtain information about Windows NT group” error (Msg 15404), try executing EXEC dbo.sp_changedbowner to change the database owner.
  • You may need to enable CLR support on your SQL Server instance using: sp_configure 'clr enabled', 1; RECONFIGURE;

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Deployment Steps

To deploy CLR objects in SQL-APIConsumer, follow these steps:

  1. Confirm CLR is enabled in your SQL instance.
  2. Set your database to TRUSTWORTHY mode.
  3. Create required assemblies like System.Runtime.Serialization and Newtonsoft.Json.
  4. Copy DLL files into an accessible CLR folder.
  5. Create the assembly and corresponding stored procedures in your SQL environment.

Conclusion

By following the above steps and utilizing the SQL-APIConsumer project, API integration becomes a breeze. Enhance your SQL Server’s capabilities by consuming web APIs effortlessly.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox