Gov. Charlie Baker will file legislation allowing Uber, Lyft and other app-based ride-for-hire services to legally operate in the state after months of heated public hearings.

“Massachusetts has always been at the forefront of emerging technologies, with this legislation providing a modern regulatory framework to embrace innovation and protect and serve consumers,” said Baker in a statement. “A diverse transportation network is key to our future economic growth and our collaboration will ensure customers have safe and reliable choices when they travel.”

The bill requires Uber and Lyft to get permits from the state, and to issue certificates to all their drivers.

The legislation will require drivers to have full background checks, and also requires the companies to give driver information to the state for another background check. The state would also be able to bar drivers from working for Uber and Lyft.

Drivers for so-called Transportation Network Companies also will be required to have $1 million in insurance coverage when the driver accepts a ride, and auto insurance that meets the state’s minimum coverage requirements.

Vehicles used by Uber and Lyft would be inspected by the state under the bill.

“Governor Baker’s legislation will allow Uber to continue offering Massachusetts safe, reliable transportation options and opportunities to earn a living with greater flexibility,” said Meghan Joyce, Uber East Coast general manager in a statement.

The legislation will also create a municipal advisory group, which will include Boston, Cambridge and Somerville.

“I thank the Baker Administration for taking a step forward in creating regulatory frameworks that incorporate transportation network companies into our existing for-hire transportation ecosystem,” said Boston Mayor Martin J. Walsh

Uber and Lyft have exploded in popularity in recent years, with Uber reporting last year it had nearly 10,000 drivers in the Boston market. The company said last week tens of thousands of individuals applied to be Uber drivers in the first quarter of the year.

The companies have also been controversial around the state, with taxi drivers fiercely fighting against any move that legalizes and codifies Uber and Lyft. Some cities and towns, including Braintree, are exploring options to require the companies to conform to some existing taxi regulations.