The US Congress has passed a stop-gap spending bill to keep the government running an extra week.
It will now go to the White House for the president's signature, and will buy Congress time to reach a deal to fund the government until September.
It came just hours before a funding deadline expired, threatening a second government shutdown in four years.
Republicans were forced to make several concessions, the latest on funding for so-called Obamacare.
A government shutdown would close national parks and monuments, lay off federal employees and delay tax refund payments.
The last shutdown, in 2013, lasted for 17 days.
What is at stake?
The bill gives lawmakers until 5 May to work out their differences on $1 trillion (£770bn) of funding until 30 September.
Without the extension, federal agencies would have run out of money by midnight on Friday (04:00 GMT Saturday).
A shutdown would most probably have triggered abrupt layoffs of hundreds of thousands of federal government workers until funding resumes.
What was the hold-up?
There have been a number of key disputes.
One was Mr Trump's proposal to erect a wall on the US-Mexico border. He had to yield to Democratic demands not to include funding for it in the spending bill.
Another was Obamacare, formally known as the Affordable Care Act.
Mr Trump made scrapping it a key campaign pledge but he failed in an earlier attempt to get his own health plan through Congress.
Congressional Republicans had hoped to revive their health legislation this week, but delayed a vote after they were unable to secure enough support for its passage.
They also wanted to include cuts to a key part of Obamacare in the budget deal but have now withdrawn that.
Democrats had refused to support the bill unless it allowed for an Obamacare provision that paid health insurance companies to help keep medical costs down for low-income Americans.
It is also not clear whether the Republicans will be able to increase defence spending without also raising investment in other domestic programmes.
Mr Trump has proposed $30bn in extra funds for the Pentagon for the rest of this fiscal year.
The resolution will also extend healthcare benefits for retired union coal miners through to 5 May. Those benefits would have expired on Friday as well.
The sweeping spending package combines 11 unfinished spending bills into a single "omnibus" bill, becoming the first bipartisan legislation under Mr Trump's presidency.