REST API POST with Authorization Token

Hi all

I’m trying to use the REST API in HA (latest version) to call the following API and grab by vehicle data.
However I’m struggling to get the API lookup working using the necessary token.

DVLA Vehicle Enquiry Service API | DVLA API Developer Portal (driver-vehicle-licensing.api.gov.uk)

I have a token in secrets.yaml as:
api_dvla: Bearer uniqueapikey

I’ve also specified the necessary config in configuration.yaml (split to a separate file). i’ve tested it with either the Authorization and x-api-key headers, neither work.

  dvla_lookup:
    url: https://driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1/vehicles
    method: POST
    headers:
      x-api-key: !secret api_dvla
      authorization: !secret api_dvla
      content_type: "application/json"
    payload: '{ "registrationNumber": "ER19BAD" }'

The response I keep getting is, missing = sign in authorization key.

I’ve hunted all over and can’t find a guide that includes REST using POST and an authorization API key so any help would be appreciated.

I’ve seen alternatives using cURL but wanted to use the built in REST functionality rather than using a cURL command.

if anyone has done this and got it to work, a copy example would be fantastic.

Kind regards

Did you try the example with curl?

curl -L -X POST 'https://driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1/vehicles' \
-H 'x-api-key: REPLACE WITH YOUR API KEY' \
-H 'Content-Type: application/json' \
-d '{"registrationNumber": "TE57VRN"}'

I don’t see any mention in the docs of adding “Bearer” before the API key.

Hi @rccoleman

Thanks for the quick reply.

The link to the bearer inclusion is here: RESTful Sensor - Home Assistant (home-assistant.io)

I did, as per the end of my query but was looking to avoid another cURL sensor in my config.

Kind regards

My point was to test using the example command line that they offered to see if it works for you, and gives you a template for how to construct your sensor. Just because the HA docs suggest a certain header configuration doesn’t mean that it’s suitable for a given service. You’re also using url:, where the documentation says to use resource:. And you’ve used content_type instead of Content-Type.

I suggest that you try this as-is:

  dvla_lookup:
    resource: https://driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1/vehicles
    method: POST
    headers:
      x-api-key: "yourAPIkeywithnoextrastuff"
      Content-Type: "application/json"
    payload: '{ "registrationNumber": "ER19BAD" }'

Thanks, I’ve just added that and removed all other config.
There are no errors in the logs so looking better.

Is there a way to check it’s working from inside HA or do I now just need to add sub-sensors?

Thanks for the help and sorry if they’re ‘simple’ questions!

The more I look at your attempt above and the docs, I’m not sure where that syntax came from. A REST sensor for your resource should look like this:

sensor:
  - platform: rest
    resource: https://driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1/vehicles
    name: dvla_lookup
    method: POST
    headers:
      x-api-key: "yourAPIkeywithnoextrastuff"
      Content-Type: "application/json"
    payload: '{ "registrationNumber": "ER19BAD" }'

And I would expect it to create a sensor entity called sensor.dvla_lookup with the response. There should be no need to create any “sub-sensors” to see the data - it would be in the state of that sensor.

Hi Rob

That’s originally where I started setting this up actually and must have got it wrong because it didn’t work either.
However I’ve done it again and it’s now working properly, thank you for the assistance.

For anyone else who has the issue, here is my setup:

  - platform: rest
    resource: https://driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1/vehicles
    name: dvla_lookup
    method: POST
    headers:
      x-api-key: !secret mysecretname
      Content-Type: "application/json"
    payload: '{ "registrationNumber": "ER19BAD" }'
    json_attributes:
      - motExpiryDate
      - taxDueDate
    unique_id: dvla_lookup
    value_template: "OK"
  - platform: template
    sensors:
      dvla_lookup_motstatus:
        friendly_name: "Vehicle MOT Status"
        value_template: "{{ state_attr('sensor.dvla_lookup', 'motExpiryDate') }}"
      dvla_lookup_taxstatus:
        friendly_name: "Vehicle Tax Status"
        value_template: "{{ state_attr('sensor.dvla_lookup', 'taxDueDate') }}"

One last question, I’m trying to convert the date that returns, in format 2022-12-01, to dd/mm/yyyy.
Any chance of assistance in updating the value template to change the date format?

I’ve got it to work in Dev > Templates but if you copy from there into the value template it doesn’t work.

From Dev Tools I’ve used the following, which returns 01/12/2022:
{{ strptime(state_attr('sensor.dvla_lookup', 'taxDueDate'), '%Y-%m-%d').strftime('%d/%m/%Y') }}

However in File Editor it states that , was not expected.

Ultimately I’d also like to add this to a calendar, not sure if that’s possible :slight_smile:

Kind regards

I don’t see anything wrong with that expression, so I don’t know why File Editor would object to it. Calendars are created by integrations (like Google Calendar) and you would use a service offered by that integration to add an event to it, such as this.

Thanks.
I restarted HA today and it accepted the same line above. All sorted :slight_smile: