The CIA’s most endangered employee for much of the past year was not an operative on a mission abroad, but an analyst who faced a torrent of threats after filing a whistleblower report that led to the impeachment of President Trump.

The analyst spent months living in no-frills hotels under surveillance by CIA security, current and former U.S. officials said. He was driven to work by armed officers in an unmarked sedan. On the few occasions he was allowed to reenter his home to retrieve belongings, a security team had to sweep the apartment first to make sure it was safe.

The measures were imposed by the CIA’s Security Protective Service, which monitored thousands of threats across social media and Internet chat rooms. Over time, a pattern emerged: Violent messages surged each time the analyst was targeted in tweets or public remarks by the president.

“The president was tweeting, ‘Where’s the whistleblower? Where’s the whistleblower?’ ” said a former senior U.S. official involved in overseeing the protection of the analyst, whose name has not been disclosed by the government. The analyst was never in direct danger, the official said, but some threats were so serious that without security, “there is a strong possibility that grave harm would have come to him.”