WASHINGTON — President Trump will deliver his State of the Union address to Congress on Feb. 5, 10 days before the deadline for lawmakers in the House and the Senate to reach an agreement on a border security package to avert another government shutdown.

Speaker Nancy Pelosi sent an invitation to the president on Monday afternoon, after the two spoke for about 12 minutes by telephone in a conversation that she initiated, an aide said. Mr. Trump later replied, agreeing to the date.

“We have a great story to tell and yet, great goals to achieve!” he wrote.

The invitation capped weeks of back-and-forth between the speaker and the president over when, and whether, he could deliver the speech in the House chamber.

On Jan. 23, in the middle of the 35-day government shutdown, Ms. Pelosi told the president she wanted to postpone the address, initially scheduled for Tuesday, until after the government reopened, and even went as far as to suggest that he deliver the speech in writing.