PublicFunctions/Get-FMPKeyMetrics.ps1

function Get-FMPKeyMetrics { 
 

    <#
        .SYNOPSIS
            Retrieves key financial metrics data for a specified company using the Financial Modeling Prep API.

        .DESCRIPTION
            The Get-FMPKeyMetrics function fetches comprehensive financial metrics for a given company based on its stock symbol.
            These metrics include important financial ratios and indicators such as PE ratio, PB ratio, debt-to-equity, ROE, ROA,
            free cash flow, and many others. Users can specify whether to retrieve annual or quarterly metrics and limit the number
            of results. If no API key is provided, the function attempts to retrieve it using the Get-FMPCredential function and
            prompts the user if necessary.

        .PARAMETER Symbol
            Specifies the stock symbol for which to retrieve key metrics data (e.g., AAPL). This parameter is mandatory.

        .PARAMETER Period
            Specifies the reporting period type. Valid values are "annual" or "quarter". The default value is "annual".

        .PARAMETER Limit
            Specifies the maximum number of key metrics records to retrieve. The default value is 5.

        .PARAMETER ApiKey
            Specifies your Financial Modeling Prep API key. If omitted, the function attempts to retrieve it using Get-FMPCredential.

        .EXAMPLE
            Get-FMPKeyMetrics -Symbol AAPL -Period annual -Limit 5

            This example retrieves the 5 most recent annual key metrics for Apple Inc.

        .EXAMPLE
            Get-FMPKeyMetrics -Symbol MSFT -Period quarter -Limit 10

            This example retrieves the 10 most recent quarterly key metrics for Microsoft Corporation.

        .NOTES
            This function utilizes the Financial Modeling Prep API's Key Metrics endpoint.
            For more information, visit: https://financialmodelingprep.com
    #>


    [CmdletBinding()]

    Param (
        [Parameter(Mandatory = $true)]
        [string] $Symbol,

        [Parameter(Mandatory = $false)]
        [ValidateSet("annual", "quarter")]
        [string] $Period = "annual",

        [Parameter(Mandatory = $false)]
        [int] $Limit = 5,

        [Parameter(Mandatory = $false)]
        [string] $ApiKey = (Get-FMPCredential)
    )

    Begin {
        if (-not $ApiKey) {
            $ApiKey = Read-Host "Please enter your Financial Modeling Prep API key"
        }
        $baseUrl = "https://financialmodelingprep.com/stable/key-metrics"
    }

    Process {
        $url = "{0}?symbol={1}&period={2}&limit={3}&apikey={4}" -f $baseUrl, $Symbol, $Period, $Limit, $ApiKey

        $headers = @{
            "Upgrade-Insecure-Requests" = "1"
        }

        try {
            $response = Invoke-RestMethod -Uri $url -Method Get -Headers $headers -ErrorAction Stop
            return $response
        }
        catch {
            throw "Error retrieving key metrics data: $_"
        }
    }
 
 };