Get deployed API Gateway ID for testing automatically

What would be the best way to save the value of the APIGateway’s ID (e.g. mb1j6zi3q0) automatically to a JSON file so it can be used by my testing suite?

I basically deploy a stack to AWS temporarily, run my tests against those deployed functions and tear it down.

Only issue is that the ID changes every deployment, so I need a way to save it to a file after each deploy to use it in my tests.