Sample code for 30+ languages & platforms
SQL Server

Finnhub API - Get Stock Quote

See more AI Examples

Demonstrates how to get a stock quote from the Finnhub API.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    -- Replace with your actual Finnhub API key.
    DECLARE @apiKey nvarchar(4000)
    SELECT @apiKey = 'YOUR_FINNHUB_API_KEY'
    DECLARE @symbol nvarchar(4000)
    SELECT @symbol = 'AAPL'

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    -- This is the URL without params.
    DECLARE @urlWithoutParams nvarchar(4000)
    SELECT @urlWithoutParams = 'https://finnhub.io/api/v1/quote'

    DECLARE @req int
    EXEC @hr = sp_OACreate 'Chilkat.HttpRequest', @req OUT

    -- Add params that will be sent in the URL.
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'symbol', @symbol
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'token', @apiKey

    EXEC sp_OASetProperty @req, 'HttpVerb', 'GET'

    -- Send the request to get the JSON response.
    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    EXEC sp_OAMethod @http, 'HttpReq', @success OUT, @urlWithoutParams, @req, @resp
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @req
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OAMethod @resp, 'GetBodyJson', @success OUT, @json

    DECLARE @statusCode int
    EXEC sp_OAGetProperty @resp, 'StatusCode', @statusCode OUT

    PRINT 'response status code: ' + @statusCode

    EXEC sp_OASetProperty @json, 'EmitCompact', 0
    EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    -- Sample result:

    -- {
    --   "c": 248.8,
    --   "d": -4.09,
    --   "dp": -1.6173,
    --   "h": 255.493,
    --   "l": 248.07,
    --   "o": 253.9,
    --   "pc": 252.89,
    --   "t": 1774641600
    -- }

    IF @statusCode = 200
      BEGIN
        -- Add the symbol to the top of the result.
        EXEC sp_OAMethod @json, 'AddStringAt', @success OUT, 0, 'symbol', @symbol

        -- Rename members for clarification.
        EXEC sp_OAMethod @json, 'Rename', @success OUT, 'c', 'currentPrice'
        EXEC sp_OAMethod @json, 'Rename', @success OUT, 'd', 'change'
        EXEC sp_OAMethod @json, 'Rename', @success OUT, 'dp', 'percentChange'
        EXEC sp_OAMethod @json, 'Rename', @success OUT, 'h', 'high'
        EXEC sp_OAMethod @json, 'Rename', @success OUT, 'l', 'low'
        EXEC sp_OAMethod @json, 'Rename', @success OUT, 'o', 'open'
        EXEC sp_OAMethod @json, 'Rename', @success OUT, 'pc', 'prevClose'
        EXEC sp_OAMethod @json, 'Rename', @success OUT, 't', 'unixTime'

        EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
        PRINT @sTmp0

      END
    ELSE
      BEGIN

        PRINT 'Failed'
      END

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @req
    EXEC @hr = sp_OADestroy @resp
    EXEC @hr = sp_OADestroy @json


END
GO