[What you need to know to start the day:Get New York Today in your inbox.]

The rapper and Instagram star known as Tekashi 6ix9ine has pleaded guilty to charges stemming from his role in a violent drug-trafficking gang and was cooperating with federal prosecutors, according to newly unsealed court records.

The rapper, whose legal name is Daniel Hernandez, admitted at a federal court hearing in Manhattan on Jan. 23 to being a member of the Nine Trey Gangsta Bloods, and said he had helped other gang members rob people at gunpoint. In one case, he said, “I helped members of Nine Trey attempt to kill a rival gang member.”

Mr. Hernandez added, “I did this to maintain or increase my own standing in Nine Trey.” He also said the gang engaged in “shooting at people, robbing people and, at times, drug trafficking.”

Mr. Hernandez said he apologized to the judge and “to anyone who was hurt, to my family, friends and fans for what I have done and who I have let down.”

Later that month, the government said, a member of the gang shot at a rival inside Barclays Center, where Mr. Hernandez was scheduled to perform. Though no one was hit, he later posted a video of himself and co-defendants bragging about the shooting, the prosecutors said.

At the time, Lance Lazzaro, a lawyer for Mr. Hernandez, pronounced him “completely innocent” of the charges, portraying him as a victim and noting that the F.B.I. had warned his client of threats made against him.

On Friday, Dawn M. Florio, another of Mr. Hernandez’s lawyers, declined to comment on his decision to plead guilty and cooperate, but added that “every effort” was being made “to protect him and his family and his loved ones, for their safety.”

Prosecutors declined to comment on the case.

In court last week, Mr. Hernandez pleaded guilty to racketeering conspiracy and eight other charges, for which he could face a minimum of 47 years in prison and a maximum sentence of life imprisonment.

If he cooperates successfully, the government has agreed to write to the judge, seeking a sentence below the minimum, a prosecutor said.

Mr. Hernandez has spent the better part of his adult life crafting a viral internet personality. His brash stunts — which included dying his hair rainbow colors and wearing clothing emblazoned with obscenities — made him a near-instant superstar on Instagram and other platforms.

[Read more aboutthe rapid rise and sudden fall of Tekashi 6ix9ine.]

But his meteoric career trajectory as a rapper was unusual. Mr. Hernandez only began pivoting to music after he had become well-known on the internet for his obnoxious antics. He appears to have sought gang affiliation to bolster his credibility among some hip-hop fans.