how to allow access to url only using a script on my server?

I have an url I have a script on each page that calls this url after page is loaded and certain conditions are met. I would like to restrict access to this url only if it is called from my domain How to achieve this? Maybe with a rule in htaccess? Thank you