Open balena internal API for login, getting a valid token internally

Hi there guys,

I am trying to make a login via a console application, while trying to make the API with the curl inside the openbalena-api container, and for every request I get a redirect to my public redirect API_HOST, which has been set in environment variable.

On the official documentation page, I could not find the proper information.

Tried different approaches: API Key as Bearer token and Service tokens.

Here is an output of what I’m getting when trying to login via curl on the same container of the API:

root@538ed6bca06c:/usr/src/app# curl -i -L -X GET --location 'http://localhost/device' --header 'AdminBalenaAPIKey: MY_API_KEY_HERE'

HTTP/1.1 301 Moved Permanently

Location: https://api.iot.mydomain.com/device

Vary: Accept

Content-Type: text/plain; charset=utf-8

Content-Length: 72

Date: Wed, 13 Dec 2023 15:39:37 GMT

Connection: keep-alive

Keep-Alive: timeout=5

HTTP/1.1 404 Not Found

X-Frame-Options: DENY

X-Content-Type-Options: nosniff

Content-Security-Policy: default-src 'none'

Content-Type: text/html; charset=utf-8

Content-Length: 145

Vary: Accept-Encoding

Date: Wed, 13 Dec 2023 15:39:37 GMT

Keep-Alive: timeout=5

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="utf-8">

<title>Error</title>

</head>

<body>

<pre>Cannot GET /device</pre>

</body>

</html>

root@538ed6bca06c:/usr/src/app#

Does anyone have an example of a curl request to OpenBalena API? I am looking for login request example, getting a token and revalidate it.

Thanks, looking forward!

@elsevero where do you get your token from?

On the other hand, did you try this? Postman

Hi

Any particular reason you’re not just using the public API url?
I think running it from within the container will only make things more complicates because of the redirects. They turn POST requests automatically into GET requests.

WIth the public url you can get a session token this way:

curl  -X POST \
  'https://api.yourdomain.com/login_' \
  --header 'Content-Type: application/json' \
  --data-raw '{
  "username": "<username>",
  "password": "<password>"
}'

And then verify the user with something like this:

curl  -X GET \
  'https://api.yourdomain.com/user/v1/whoami' \
  --header 'Authorization: Bearer <session-token>
1 Like