Image copyright Getty Images Image caption The pair have had a contentious relationship throughout the race

Defeated Republican presidential hopeful Ted Cruz has endorsed Donald Trump in the race for the White House.

The Texas senator fought Mr Trump in a bitter primary battle, marked by mud-slinging and personal insults.

Mr Cruz said he would fulfil his promise to vote for the Republican nominee and that electing Hillary Clinton would be "wholly unacceptable".

He drew ire at the Republican National Convention in July, when he was booed off stage for not endorsing Mr Trump.

"This election is unlike any other in our nation's history. Like many other voters, I have struggled to determine the right course of action in this general election," Mr Cruz announced in a Facebook post.

"After months of careful consideration, of prayer and searching my own conscience, I have decided that on Election Day, I will vote for the Republican nominee, Donald Trump."

Everything Ted Cruz has said about Trump

The comments underscore a dramatic U-turn for Mr Cruz, who has referred to Mr Trump as a "trainwreck" and a "pathological liar" who could not be trusted in the White House.

Mr Trump responded to the Cruz reversal by saying he was "greatly honoured" to have the endorsement of "a tough and brilliant opponent".

But some of Cruz supporters decried the senator's announcement, including his former campaign spokesman, Rick Tyler, who told NBC: "It's mourning in America for conservatives. We lost our leader today."

Meanwhile, Trump campaign manager, Kellyanne Conway, who previously worked for a super PAC supporting Mr Cruz, expressed her relief on social media.

Image copyright Twitter

Analysis - Anthony Zurcher, BBC News, Washington

By endorsing Donald Trump now - after all the heat he took for his "vote your conscience" stand in July - Mr Cruz appears to be acknowledging two realities.

The first is that Mr Trump might actually win the presidency even without Mr Cruz's support.

At the very least, he will be competitive, diminishing the Texas senator's ability to tell his fellow party mates "I told you so".

The second is that Trump-ism is the future of the Republican Party - and if Mr Cruz wants to be a part of that, he's going to have to play ball.

The Texas senator has to run for re-election in 2018, and he faces the threat of a pro-Trump primary challenger. This endorsement could be a way to defuse that political bomb.

Although he styles himself as an ideological true believe, Mr Cruz has shown remarkable political flexibility.

The Ivy League establishment lawyer turned into anti-Washington firebrand to win a Senate seat. During the presidential campaign, he embraced Mr Trump when he saw it in his interest, then turned on the front-runner when the field narrowed.

Now, it seems, the Trump-Cruz bromance has been rekindled. But the Republican nominee would be wise to watch his back.

Mr Cruz said he decided to back his former foe due to six key policy differences between Mr Trump and Hillary Clinton, including the fate of the Supreme Court, Obamacare, immigration and energy.

Mr Trump and Mr Cruz repeatedly clashed during the primary campaign both on and off the debate stage.

Mr Trump came under fire after he tweeted an unflattering photo of Mr Cruz's wife, Heidi.

He also suggested that Mr Cruz's father, Rafael Cruz, was connected to the assassination of US President John F Kennedy.

Media playback is unsupported on your device Media caption The moment the crowd turned on Ted Cruz

Just before the Indiana primary election, Mr Cruz called the New York businessman a "braggadocios, arrogant buffoon" and a "serial philanderer".

At the Republican National Convention in Ohio, Mr Cruz refused to throw his support behind the official Republican nominee while giving a speech during a primetime slot.

He instead told Americans to "vote your conscience" in November and was subsequently booed off the stage.

After the event, Mr Cruz said: "I am not in the habit of supporting people who attack my wife and attack my father."

In announcing his decision on Friday, Mr Cruz emphasised that he was focused on preventing Mrs Clinton from winning the White House.

"Our country is in crisis. Hillary Clinton is manifestly unfit to be president, and her policies would harm millions of Americans," he said.

"And Donald Trump is the only thing standing in her way."

The news comes as Mr Trump prepares to face Mrs Clinton in the first presidential debate on Monday at Hofstra University in New York.