Can I call external APIs to use their data in-game?

Godot Version 4.3

Question

Generally, is it possible to communicate with external APIs from GDscript, to use external data in-game? Is there an existing framework to do so ? Or at least, maybe read data from a csv, in which I will drop data from a refularly scheduled script that will read the API ?

More specifically, for instance, I am trying to get sport data of a user have an impact in-game. I found the Strava API, which requires “a Java runtime installed on your machine to run Swagger”. Is that doable ?

Another instance would be weather API to match current IRL weather in-game, for instance.

Is this what you mean, this is a very rough outline, I don’t use the service but it seems you will need to make sure you don’t hardcode in your token and secret. I would suggest using a environment variable and obfuscate it. Doing that though you will need to deobfuscate when loading. One more side note if you use github,lab,tea make sure you never plain text any keys, tokens , secrets etc.

extends Node

# Replace with your own credentials
const CLIENT_ID = "YOUR_CLIENT_ID"
const CLIENT_SECRET = "YOUR_CLIENT_SECRET"
const ACCESS_TOKEN = "YOUR_ACCESS_TOKEN"  # Obtain this after authentication

const STRAVA_API_URL = "https://www.strava.com/api/v3"

func _ready():
    # Authenticate and obtain access token (optional)
    # authenticate()

    # Retrieve activities
    get_activities()

func authenticate():
    # Step 1: Obtain authorization code
    var auth_url = "https://www.strava.com/oauth/authorize?"
    auth_url += "client_id=" + CLIENT_ID
    auth_url += "&redirect_uri=http://localhost"
    auth_url += "&response_type=code"
    auth_url += "&scope=activity:read"

    var http_request = HTTPRequest.new()
    add_child(http_request)
    http_request.request(auth_url, [], true, HTTPClient.METHOD_GET)

    # Step 2: Exchange authorization code for access token
    http_request.connect("request_completed", _on_auth_completed)

func _on_auth_completed(result, response_code, headers, body):
    if result == HTTPRequest.RESULT_SUCCESS:
        var json = parse_json(body.get_string_from_utf8())
        var access_token = json.access_token
        # Use access token for subsequent requests
        ACCESS_TOKEN = access_token

func get_activities():
    var url = STRAVA_API_URL + "/activities"
    var headers = ["Authorization: Bearer " + ACCESS_TOKEN]
    var http_request = HTTPRequest.new()
    add_child(http_request)
    http_request.request(url, headers, true, HTTPClient.METHOD_GET)

    http_request.connect("request_completed", _on_activities_completed)

func _on_activities_completed(result, response_code, headers, body):
    if result == HTTPRequest.RESULT_SUCCESS:
        var json = parse_json(body.get_string_from_utf8())
        print(json)  # List of activities
        for activity in json:
            print(activity.name)