The Trump administration on Friday gave New York City and eight other local governments until June 30 to prove they aren’t violating federal law by giving official sanctuary to illegal immigrants.

The ultimatums from the US Department of Justice followed repeated threats by the feds to cancel massive anti-crime grants, including $4.3 million to the Big Apple for the current fiscal year.

In announcing the deadline, the DOJ said many of the targeted localities were “crumbling under the weight of illegal immigration and violent crime.”

“New York City continues to see gang murder after gang murder, the predictable consequence of the city’s ‘soft on crime’ stance,” the DOJ said.

A related letter to the director of Mayor de Blasio’s Office of Criminal Justice warned that in addition to jeopardizing its federal funding, refusal to comply could result in “other action, as appropriate.”

Similar letters went to ​officials in Chicago; New Orleans; Philadelphia; Miami-Dade County; Clark County, Nev.; Milwaukee County, Wisc.; Cook County, Ill.; and the state of California.

The grant money at issue is provided through a program named in memory of slain New York City cop Eddie Byrne, who was ambushed in 1988 while protecting a witness who had agreed to testify against local drug dealers in Queens.

During a hastily arranged news conference at One Police Plaza, NYPD Commissioner James O’Neill said his “blood began to boil” while reading the DOJ statement.

O’Neill said cops locked up more than 1,000 gang members last year, adding: “Maybe we should ask them if we’re soft on crime.”

At the same news conference, Mayor de Blasio also challenged President Trump and US Attorney General Jeff Sessions to “come here to New York City and look our police officers in the eye and tell them you believe they are soft on crime.”

In a prepared statement, the head of the NYPD sergeants union, Ed Mullins, said Sessions was “absolutely correct to hold New York and other jurisdictions accountable for their so-called sanctuary policies.”

“I just hope the mayor will finally come to his senses and stop ignoring and harboring violent criminals before it’s too late,” he added.