Use Rest API to Run a Script AND Send it Some Data

I’ve been trying for about 3 hours to figure this out, and no amount of trial and error, documentation reading, or forum/internet searching has yeilded an answer. I found a few posts here with people asking similar questions, but either the solution isn’t helping me or nobody ever answered the question.

Here’s what I want to do. I want to use the Rest API to run a script, but I want to pass some data to the script from the external call. Here’s what I have for a CURL command (for testing):

curl \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer MYTOKEN" \
  -d '{"entity_id": "script.launch_streaming_video","the_url": "https://www.disneyplus.com/video/afdc98f1-26bf-48d8-8866-af185ba5d5ac"}' \
  http://MY_IP/api/services/script/turn_on

And the script:

alias: Launch Streaming Video
fields:
  the_url:
    description: The URL to the episode or movie
    example: https://www.disneyplus.com/video/afdc98f1-26bf-48d8-8866-af185ba5d5ac
sequence:
  - service: media_player.play_media
    data:
      media_content_type: url
      media_content_id: "{{ the_url }}"
    target:
      device_id: 6c498452766127570b04aefeffcee4a9
mode: single
icon: mdi:television

The script works fine if I call it from the HA developer tools and pass a URL. If I try it from CURL I get a 400: Bad Request response. If I remove the second item from the JSON (i.e. the_url), it “works” but with no data passed to the script.

Is there a way to call a script and pass it some data?

Of course two minutes after I post this I try one more search and find a post that finally helps me. Here’s the CURL that will do what I want:

curl \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer MY_TOKEN" \
  -d '{"entity_id": "script.launch_streaming_video","variables":{"the_url": "https://www.disneyplus.com/video/afdc98f1-26bf-48d8-8866-af185ba5d5ac"}}' \
  http://MY_IP/api/services/script/turn_on

The trick seems to be that the JSON sent has to have the additional stuff in a variables list.

1 Like

Just to complete the answer a little bit (I was still struggling to adopt this for other services):

When a service description ( the section in GET on /api/services corresponding to the service you’re trying to invoke) mentions fields, you are supposed to place those fields in the same level as the target, i.e.

 "volume_mute": {
                "description": "Mutes or unmutes the media player.",
                "fields": {
                    "is_volume_muted": {
                        "description": "Defines whether or not it is muted.",
                        "name": "Muted",
                        "required": true,
                        "selector": {
                            "boolean": null
                        }
                    }
                },
                "name": "Mute/unmute volume",
                "target": {
                    "entity": [
                        {
                            "domain": [
                                "media_player"
                            ],
                            "supported_features": [
                                8
                            ]
                        }
                    ]
                }
            }

Is supposed to be invoked with a JSON body like this:

 {  
     "entity_id": "media_player.living_room", 
     "is_volume_muted": true 
}

Took me an hour to figure that out