The Republican party-controlled US House of Representatives has voted to extend short-term funding for the Department of Homeland Security for just one week, hours after they unexpectedly rejected the budget outright and increased the prospect of a partial shutdown of the agency.
Friday's late night vote was a last ditch effort to avoid the shutdown, after the House had earlier voted 224-203 against the measure, as 52 Republicans defected on the legislation backed by their leaders.
But at 03:00 GMT, the House passed a one-week funding extension, with the measure now going to President Barack Obama for signing.
The Department of Homeland Security had earlier circulated a lengthy contingency plan.
The 46-page document indicated that more than 30,000 employees would face furloughs if Congress did not vote to extend funding.
A combination of conservative, tea party-backed Republicans on one side of the political aisle, and Democrats on the other had earlier opposed the bill.
The first group was upset because the legislation had been stripped of changes to Obama's immigration policy, and the second because it lacked full-year funding for the sprawling department.
It does not make any difference whether the funding is for three weeks, three months or a full fiscal year. If it's illegal, it's illegal.
Much of the Department of Homeland Security was to remain open, even if funding were to have expired at midnight.
Airport security checkpoints would have remained staffed, immigration agents would be on the job, air marshals would do their work and Coastguard patrols would sail on.
Of the department's 230,000 employees, an estimated 200,000 would have remained at work, either because they are deemed essential, or because their pay comes from fees that are unaffected by congressional spending disputes.
Democrats led by Representative Nancy Pelosi had earlier urged Republican leaders in advance to allow a vote on a bill to keep the department in funds through the September 30 end of the budget year - a step the Republican high command had previously refused to take.
"You have made a mess," Pelosi said to Republicans as debate neared to an end on the measure.
Some tea-party backed representatives disagreed.
"It does not make any difference whether the funding is for three weeks, three months or a full fiscal year. If it's illegal, it's illegal," said Congressman Mo Brooks, a Republican, referring to Obama's actions.
Across the Capitol, the Senate waited to add its assent after playing out a series of acts in the Republicans' effort to use the measure to wring concessions on immigration from the White House.
A largely symbolic attempt to advance legislation that would repeal Obama's immigration directive of last autumn failed on a vote of 57-42, three short of the 60 required in the 100-member chamber.
Taken together, the day's events at the Capitol underscored the difficulty Republicans have had so far this year in translating last year's election gains into legislative accomplishment - a step its own leaders say is necessary to establish its credentials as a responsible, governing party.
Republicans gained control of the Senate in last November's balloting, and emerged with their largest House majority in more than 70 years.