San Francisco filed a lawsuit on Tuesday challenging President Donald Trump's executive order directing the US government to withhold money from cities that have adopted sanctuary policies toward undocumented immigrants.

The lawsuit, filed by San Francisco City Attorney Dennis Herrera, marks the first court challenge over the sanctuary order filed by one of the targeted cities.

Mr Trump signed the directive on sanctuary cities on 25 January, along with an executive order to build a wall along the US-Mexican border, as he charged ahead with sweeping and divisive plans to transform how the US deals with immigration and national security.

Local officials in cities such as New York, Los Angeles, Chicago, Philadelphia, Boston, Denver, Washington and Seattle, as well as San Francisco, offer some forms of protection to illegal immigrants.

Donald Trump supporters boo mention of immigrants during speech

Billions of dollars in federal aid to those cities could be at risk.

Tuesday's lawsuit alleges that the executive order violates the 10th Amendment of the US Constitution, which states that powers not granted to the federal government should fall to the states.

"In blatant disregard of the law, President of the United States seeks to coerce local authorities into abandoning what are known as 'Sanctuary City' laws and policies," said the lawsuit, filed in San Francisco federal court.

The controversial orders Donald Trump has already issued Show all 9 1 /9 The controversial orders Donald Trump has already issued The controversial orders Donald Trump has already issued Trump and the media White House Press Secretary Sean Spicer takes questions during the daily press briefing Getty Images The controversial orders Donald Trump has already issued Trump and the Trans-Pacific Partnership Union leaders applaud US President Donald Trump for signing an executive order withdrawing the US from the Trans-Pacific Partnership negotiations during a meeting in the Roosevelt Room of the White House in Washington DC. Mr Trump issued a presidential memorandum in January announcing that the US would withdraw from the trade deal Getty The controversial orders Donald Trump has already issued Trump and the Mexico wall A US Border Patrol vehicle sits waiting for illegal immigrants at a fence opening near the US-Mexico border near McAllen, Texas. The number of incoming immigrants has surged ahead of the upcoming Presidential inauguration of Donald Trump, who has pledged to build a wall along the US-Mexico border. A signature campaign promise, Mr Trump outlined his intention to build a border wall on the US-Mexico border days after taking office Getty Images The controversial orders Donald Trump has already issued Trump and abortion US President Donald Trump signs an executive order as Chief of Staff Reince Priebus looks on in the Oval Office of the White House. Mr Trump reinstated a ban on American financial aide being granted to non-governmental organizations that provide abortion counseling, provide abortion referrals, or advocate for abortion access outside of the United States Getty Images The controversial orders Donald Trump has already issued Trump and the Dakota Access pipeline Opponents of the Keystone XL and Dakota Access pipelines hold a rally as they protest US President Donald Trump's executive orders advancing their construction, at Columbus Circle in New York. US President Donald Trump signed executive orders reviving the construction of two controversial oil pipelines, but said the projects would be subject to renegotiation Getty Images The controversial orders Donald Trump has already issued Trump and 'Obamacare' Nancy Pelosi who is the minority leader of the House of Representatives speaks beside House Democrats at an event to protect the Affordable Care Act in Los Angeles, California. US President Donald Trump's effort to make good on his campaign promise to repeal and replace the healthcare law failed when Republicans failed to get enough votes. Mr Trump has promised to revisit the matter Getty Images The controversial orders Donald Trump has already issued Donald Trump and 'sanctuary cities' US President Donald Trump signed an executive order in January threatening to pull funding for so-called "sanctuary cities" if they do not comply with federal immigration law AP The controversial orders Donald Trump has already issued Trump and the travel ban US President Donald Trump has attempted twice to restrict travel into the United States from several predominantly Muslim countries. The first attempt, in February, was met with swift opposition from protesters who flocked to airports around the country. That travel ban was later blocked by the Ninth Circuit Court of Appeals. The second ban was blocked by a federal judge a day before it was scheduled to be implemented in mid-March SANDY HUFFAKER/AFP/Getty Images The controversial orders Donald Trump has already issued Trump and climate change US President Donald Trump sought to dismantle several of his predecessor's actions on climate change in March. His order instructed the Environmental Protection Agency to reevaluate the Clean Power Plan, which would cap power plant emissions Shannon Stapleton/Reuters

Mr Herrera told reporters at City Hall on Tuesday morning the city's sanctuary policy was borne out of a desire to allow and encourage undocumented immigrants to report crimes to police without fear of being deported.

He said such policies make residents safer and cited research suggesting that counties and cities with sanctuary policies, of which he estimated there were 400 across the country, had fewer crimes per 10,000 residents than other jurisdictions.

"President Trump's executive order tries to turn city and state employees into federal immigration officers. That is unconstitutional," Mr Herrera said.

The suit seeks to halt Mr Trump's order and also calls on a judge to declare that San Francisco is in compliance with federal law.