Pass own data to hass.services.register

I have an api class in my custom components and data is stored in a python var.
So in init I have something like this:
def setup(hass, config):
myapi = ostrom_api.Ostrom_api()

but how do I pass the class to
hass.services.register(DOMAIN, “get_price”, handle_price_forecast)

because in
def handle_price_forecast(call):

Unfortunately myapi is invalid there.

so i need transport the myapi reference via call var - but how ?

Make a method to setup your services like this.

def setup_services(api: MyAPI):

    def handle_price_forecast(service_call):
        api.func etc.....


    hass.services.register(DOMAIN, “get_price”, handle_price_forecast)

Then to setup services in just call

setup_services(api)

nice idea - but dont work.

It does work (as a principle) as this is how i use it in an integration i write.

See line 132 in init.py and services.py

i rewrite the api with return text and use hass.data[DOMAIN] = {“apikey”: key}
i need only 5 values for the api
apikey = a base64 string
and token, expire, zip, contract_id : all strings

addon:
sensor or states > using not actual date for last_triggered / last_updated by creation ?
i get also 36hour old data :frowning: - and
if hass sensor date <> value date from api gets things tricky

thx

for own services.py i need more experience :frowning:
my first custom component