hellohq-io / hellohq.docs

1 stars 0 forks source link

Access new API for HQ 2.0 #163

Closed Kathrin001 closed 6 months ago

Kathrin001 commented 7 months ago

We access the current HQ API version v1 via Power BI based on the code described in the HQ support center. We would like to test the new API version for HQ 2.0. Currently we access the API through the following URL: https://api.hellohq.io/v1/ProjectStatuses and the API token given unter the Admin/API-Clients section. We tried to access the new API by just changing v1 into v2 and by creating a new token under Admin/Settings/API Clients and changed the entity name into ProjectStatus (instead of ProjectStatuses in this example), but get an error message in Power BI. Is there already a code example for the new version you can share with us, so that we can test the new API?

monderino commented 6 months ago

Hey @Kathrin001,

changing the version will not work because the new API follows a different style and not all endpoints are available. The v2 is actually in a beta state. You can see all endpoints via: https://beta.hellohq.io/swagger/index.html

Here the new PowerQuery Snippet:

let
    // Define constants for the base URL and the authorization token
    BaseUrl = "https://beta.hellohq.io/v2/<Endpoint>?",
    Token = "<Your Token>",
    BatchSize = 1000,

    // Define a recursive function to fetch data in chunks
    FetchData = (skip as number) =>
        let
            // Define the options for the web request, including the authorization header
            Options = [Headers=[#"Authorization" = "Bearer " & Token]],

            // Construct the URL for the web request
            Url = BaseUrl & "top=" & Text.From(BatchSize) & "&skip=" & Text.From(skip),

            // Make the web request and get the raw data
            RawData = Web.Contents(Url, Options),

            // Parse the raw data as JSON
            JsonResponse = Json.Document(RawData),

            // If the JSON response is a list, use it as is; otherwise, wrap it in a list
            JsonList = if JsonResponse is list then JsonResponse else {JsonResponse}
        in
            // If the list is empty, return an empty list; otherwise, concatenate the list with the result of the next recursive call
            if JsonList = {} then
                {}
            else
                JsonList & @FetchData(skip + BatchSize),

    // Call the recursive function to fetch all data and convert the result to a table
    Result = Table.FromList(FetchData(0), Splitter.SplitByNothing(), null, null, ExtraValues.Error)
in
    Result

I hope this will help.

Best Sven

Kathrin001 commented 6 months ago

Thanks, it works.