Guide: Getting started with the AutoPi REST API

To use the AutoPi REST API you can access the API with your browser, which will display our autogenerated documentation portal.

The documentation is auto-generated from our API, which means that the documentation will always reflect the API and will always be up to date, but being auto-generated it unfortunately also does sacrifice some readability, but we are working to improve this.
And if you find something that you feel is not adequately documented, please let us know

Authentication

If you want to test the endpoints, you can authenticate in the API documentation portal by setting the token to use when authenticating.

It is possible to authenticate using two different tokens.

API Tokens

This token has an optional expirery date, and can be generated from the “Account” page in the frontend on my.autopi.io.

It is specifically made for users who wants to make requests to the API, and is the recommended way to make authenticated requests to the API.

To use the token, send an authorization header like this, in all requests:

Authorization: APIToken YOUR_TOKEN

JWT Token (short expirery)

This token is the one used by the frontend when logging in.

  • It expires relatively shortly
  • you need to enter you username and password to acquire the token.

You can get the JWT token in two different ways.

Capture the token by using the browser developer tools.

The easiest way is to capture the token by logging into my.autopi.io, with the developer tools open in your favourite browser, with the network tab open, and then skip to step 7 in the below step by step guide.

Manually call the auth endpoint to get the token.

You can follow the steps below to call the login endpoint manually.

  1. Click the “auth” app to fold out the available endpoints.
  2. Click the “/auth/login/”
  3. Click the “try it out” button to the right.
  4. Change the payload to look like this (remove the username field, and fill out the email and password fields, like so:
   {
      "email": "your_email",
      "password": "your_insanely_complex_password"
   }
  1. Click the blue “execute” button.
  2. Now you can copy the entire token
  3. Now click the green “Authorize” button in the top right of the page and paste the token in the field.
    Remember to write “bearer” in front of the token - like so:
Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.4pXwtyQKCwSrYfcj9O7MGVv5ustPbx0GmYY7jHZL8es
  1. After clicking close, you should now be able to call the other authenticated endpoints.

Sending the requests manually using Postman or similar

Alternatively, if you are unable to use the above portal, or if you’d rather use something like Postman or similar, you can still see the requests and parameters in the portal, but to call them manually, see the below steps.

Authenticating manually w/o interactive API documentation portal
You can do the above steps manually by following these steps

  1. Authentication
    To obtain an authentication token, send a post request to
    https://api.autopi.io/auth/login/

    with data (Content-Type=application/json):

    { "email": "your email address", "password": "your password" }
    

    In the response, you will find the token used to authenticate the below requests

  2. To request data from our API, the authorization header should be set

    You will need to set the “authorization” header on the requests, that is not possible if requesting via a browser, so you must send the requests from a script, or from API tools such as postman or any other REST endpoint tool.
    To set the header, use the below values

    Header: authorization
    Value: bearer YOUR_TOKEN

    like so, for the JWT token:

    Authorization: bearer YOUR_TOKEN
    

    or if you are using the API token

    Authorization: APIToken YOUR_TOKEN
    

Using developer tools to see how endpoints are used
If you find something where you are unsure how to proceed, you can log in to my.autopi.io and use the chrome developer tools to see the requests and parameters sent by the application, and if you are still experiencing issues, you can create a topic here on the community.

Happy developing, and as always, if you run into issues, exceptions, have suggestions etc, please let us know :slight_smile:

3 Likes

Hello!

I was trying to do as mentioned in the tutorial, but if i send email und password by clicking Interact and Send request i always run into an error “Uncaught TypeError: Cannot read property ‘auth’ of undefined” (see attachment).

What am I doing wrong?

41

1 Like

Hi Peter

Ah, yeah I can see that issue as well, we will look into it.
So I think you are doing everything right, but the issue is in our end.

Thank you for reporting the error :slight_smile:

Best regards
/Malte

I have same problem, when it will be fixed?

Terminal in local i run pip.install coreapi-cli, because pip install coreapi-cli don´t working like guide…
Installation say everything is installed already, nothing done.
Commands are not working on cloud terminal either, always timeout.
But i can´t use coreapi command on terminal to get API functions working.
How i should run commands on terminal and are you planning to give command list guide somewhere or should i just try finding code and commands what to use?

Hi Hakkyou

The above commands is not used in the local or in the cloud terminal, but for your own terminal on your computer.
We are working on updating our API documentation away from using coreapi.
So for now you should disregard the sample on the right side, ie. no coreapi, and instead use something like postman to send requests to the API.

As noted above, it’s also possible to see the requests that the frontend on my.autopi.io is sending to the API.
The documentation portal at api.autopi.io should give an overview of all available endpoints, but some of the detailed parameters is not yet included in the API portal.

By the way, the issue in the portal should be fixed later today.

Best regards
/Malte

Hi,
I’m not sure I understand how to do it.
I try with Postman and I always have a login page nothings else.
Capture

Hi Reginald

The url and the method you are trying to call is incorrect, it should instead be a GET request to

https://api.autopi.io/auth/user/

The above endpoint is the one that returns the logged in user object.
(To retrieve a token, you need to send a POST request to https://api.autopi.io/auth/login/ as specified in the guide above)

But the authorization header is set correctly.
Remember that the token currently expires after 8 hours, but it will tell you in the response if the token is valid or not.

Best regards
/Malte

Ok I start to understand how it’s work.
But I want to make a Dashboard like your but with real time information and only information I Want, if the token expires after 8 hour it’s will never work.
How can I authenticate without time limitation?

1 Like

Hi Reginald

We have on our roadmap some changes to the API access, which will allow users to generate an API key for their user, that will not expire. This will allow the your dashboard to be logged in indefinitely.

Currently you will need to log in once every 8 hours.
Usually you’d make your application send a login request, maybe on startup, and then use the token for any subsequent requests, while either listening for 401 Unauthorized status codes from the responses to the API, or by logging in again every N hours automatically.

We hope to have the above API key changes ready in the next few weeks.
But as i’ve written above, there are workarounds until you can generate a API token that doesn’t expire.

Best regards
/Malte

Is there a way to get the LAST POSITION from a device through the REST API? I can get positions as mentioned in step 3 above. But if i don’t know when the device moved the last time, i can’t set a start_utc?

Maybe the REST API supports a call of “LAST POSITION”. If not, can i get the datetime of last movement to get last position?

Hi Peter

Yes, you can retrieve the last known position using this endpoint

https://api.autopi.io/logbook/most_recent_position/
  ?date_cutoff=2018-01-29T09:54:58.679Z
  &device_id=DEVICE_ID

date_cutoff: Is the max timestamp (UTC) of the logged positions. So if you want the position of the car on christmas eve, you invoke the endpoint with the date_cutoff set to the UTC timestamp of christmas eve.

device_id: Id of the device, not the printed Unit_id you received with the device, but the ID in the api.
Call the “/dongle/devices” endpoint to get the list of devices, which shows the api.

Best regards
/Malte

Hello Malte

works great. Thank you very much!

1 Like

Hello,

Tried using Python and the coreapi to interface with the AutoPi, however unsuccessfully. Managed to install coreapi but the following code snipet

import coreapi
client = coreapi.Client()
schema = client.get("https://api.autopi.io/auth/login/")

gave the error

Traceback (most recent call last):
File “AutoPi.py”, line 28, in
schema = client.get(“https://api.autopi.io/auth/login/”)
File “C:\Users\ososc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\coreapi\client.py”, line 136, in get
return transport.transition(link, decoders, force_codec=force_codec)
File “C:\Users\ososc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\coreapi\transports\http.py”, line 386, in transition
raise exceptions.ErrorMessage(result)
coreapi.exceptions.ErrorMessage: <Error: 405 Method Not Allowed>
detail: “Method “GET” not allowed.”

What could be wrong? I managed to use requests.post() to retrieve some data. But it would be nice to get coreapi working, since it seems to be more user-friendly.

Best regards,

I appreciate this guide. It helped me get started on pulling data, but I am not able to find an exhaustive list of what “keys” (for example: https://api.autopi.io/logbook/storage/data/?type=primitive&key=coolant_temp&device_id=DEVICE_ID&start_utc=2018-05-02T15:17:51.389Z&end_utc=2018-05-02T15:35:07.360Z&interval=5m ) are possible to read data from. Specifically I am interested in getting the data from the accelerometer on my AutoPi, can you guide me through that?

Hi
Yeah, we are working on making that much more transparent.

To get the accelerometer, you specify the type “accelerometer”, instead of “primitive”, and then don’t specify the key.

That should give you the saved accelerometer values, but to get the device to save the accelerometer values, you also need to create a job that loags the accelerometer values.

To do that, you should go into jobs, and create a job that calls the “acc.xyz” function, with the returner set to “cloud”.

EDIT: You no longer need to manually log the accelerometer, it is part of the default logging now. Check out the advanced settings to see the settings for the accelerometer.

Best regards
/Malte

How can i get raw data of each trip via API ? Could you guys help to advise?

Hi Sureerat

You can retrieve the trips via the API, and each trip includes a start time and an end time, which can be used to retrieve the raw data of the trips.
Check out the guide above, on how to retrieve the raw data.

Best regards
/Malte

@Malte
ok, i had download the postman, and succefull connected
19

so, now I want the request for new logger I had make, like SoC (for Ev car)
how to write it exactly same as that but for the SoC ?
I only need the result to send to mysql table, so 0 to 100 result. it is the raw data ?

Speed readings
https://api.autopi.io/logbook/storage/data/?type=primitive&key=speed&device_id=DEVICE_ID&start_utc=2018-05-02T15:17:51.389Z&end_utc=2018-05-02T15:35:07.360Z&interval=5m

@Remy_Tsuihiji this is what works for me:
https://api.autopi.io/logbook/storage/read/?from_utc=2019-03-15T22:53:56.493Z&field=obd.soc_display.value&field_type=json&device_id={your_device_id}&interval=1s&end_utc=2019-03-16T09:53:56.495Z

Times are just for example, you want to change them :slight_smile:
Also, I have no idea what field_type does. If it’s empty, I don’t get anything. If I put anything (“1” or “json” or “bla”) it spits out json formatted reply.
Strange :slight_smile:
Make sure to also include bearer token in headers! You also get the device_id when requesting for a token.