President Donald Trump said he would up for meeting Iran's president Hassan Rouhani “if the circumstances were correct or right”.

The idea was floated at the G7 summit to try to end a US-Iran nuclear impasse between the two countries.

Iran's foreign minister made a surprise appearance at the summit in Biarritz, after being invited by French President Emmanuel Macron.

US President Donald Trump appeared to agree to the meeting, saying he believed Iran needed a deal amid suggestions of a summit with Iranian president Hassan Rouhani.

“At a given point in time there will have to be a meeting between the American and Iranian president,” Mr Trump said at the close of the G7 summit, as he stood beside French President Emmanuel Macron, the host of this year’s gathering.

Mr Macron said he hoped for a meeting in the “coming weeks”.

Mr Trump said a meeting could happen with the Iranian president if the circumstances are right. Credit: Markus Schreiber/AP

But Mr Trump committed to no time frame, saying a meeting could only happen “if the circumstances were correct or right”.

Mr Macron intercepted Mr Trump as he arrived at his hotel on the first day and the two spent nearly two hours alone together on a sunny terrace, eating lunch and talking before the summit’s official start.

Mr Trump seemed almost smitten at the memory.

“He wasn’t trying to impress his people. I wasn’t trying to impress my people. We were just trying to impress each other,” he said.

Iran’s President Hassan Rouhani. Credit: AP

Mr Trump said he gave his approval for the invitation to Iranian foreign minister Mohammad Javad Zarif, despite new US sanctions against the envoy.

Mr Macron and other G7 leaders opposed Mr Trump’s decision last year to pull the United States out of a 2015 nuclear deal between Iran and world powers, and to impose new sanctions.

They have been trying to salvage the painstakingly negotiated treaty ever since.

Tensions over Iran, Russia, the US trade war with China and the faltering global economy dominated the three-day summit.

President Donald Trump and and first lady Melania Trump board Air Force One in Biarritz, France. Credit: Andrew Harnik/AP

Mr Trump insisted the gathering was marked by absolute unity, which was true to the extent that the other leaders carefully sidestepped any differences with him.

The biggest risk was the invitation to Mr Zarif, which Macron’s office said came together in a matter of hours.

“I said if you want to do that, that’s OK. I don’t consider that disrespectful at all. Especially when he asked me for approval,” Mr Trump said.

In a televised speech, Iranian President Hassan Rouhani also appeared on the defensive, shielding his foreign minister against criticism from hardliners who have rejected negotiations until sanctions are lifted.

Iran’s economy has nosedived since the US imposed new sanctions.

“If I knew that going to a meeting and visiting a person would help my country’s development and resolve the problems of the people, I would not miss it,” Mr Rouhani said.

“Even if the odds of success are not 90% but are 20% or 10%, we must move ahead with it. We should not miss opportunities.”

German chancellor Angela Merkel said an achievement of the G7 summit was an agreement to block Iran from having nuclear weapons “by political means”.

She would not list any specifics.

“What will come out of this, what possibilities will open up, we can’t say today. But the firm will to talk is already great progress,” she said.