Sample code for 30+ languages & platforms
Perl Requires Chilkat v11.4.0+

Finnhub API - Get Stock Quote

See more AI Examples

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

Chilkat Perl Downloads

Perl
use strict;
use warnings;
use Chilkat2;

# Replace with your actual Finnhub API key.
my $apiKey = 'YOUR_FINNHUB_API_KEY';
my $symbol = 'AAPL';

my $http = Chilkat2::Http->new;

# This is the URL without params.
my $urlWithoutParams = 'https://finnhub.io/api/v1/quote';

my $req = Chilkat2::HttpRequest->new;

# Add params that will be sent in the URL.
$req->AddParam('symbol', $symbol);
$req->AddParam('token', $apiKey);

$req->HttpVerb('GET');

# Send the request to get the JSON response.
my $resp = Chilkat2::HttpResponse->new;
my $success = $http->HttpReq($urlWithoutParams, $req, $resp);
if (!$success) {
    print $http->LastErrorText, "\n";
    exit;
}

my $json = Chilkat2::JsonObject->new;
$resp->GetBodyJson($json);

my $statusCode = $resp->StatusCode;
print 'response status code: ', $statusCode, "\n";

$json->EmitCompact(0);
print $json->Emit(), "\n";

# 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) {
    # Add the symbol to the top of the result.
    $json->AddStringAt(0, 'symbol', $symbol);

    # Rename members for clarification.
    $json->Rename('c', 'currentPrice');
    $json->Rename('d', 'change');
    $json->Rename('dp', 'percentChange');
    $json->Rename('h', 'high');
    $json->Rename('l', 'low');
    $json->Rename('o', 'open');
    $json->Rename('pc', 'prevClose');
    $json->Rename('t', 'unixTime');

    print $json->Emit(), "\n";
} else {
    print "Failed\n";
}