The superstar rapper was strutting on the right side of the law during a “surprise” appearance at the NYPD Police Academy in Queens, sources said.

The “Shake It” singer did just that, dancing and twirling to thunderous applause, according to a 17-second video obtained by The Post.

Cardi was there Friday for an “event” with Chief of Training Juanita Holmes, the sources said. The NYPD said the rapper was on hand for “Girl Talk,” part of a police mentorship program.

The appearance seems to be part of the “WAP” singer’s court-ordered 15 days of community service, which she announced to her 28.7 million Twitter followers, complete with a few selfies and words of wisdom.

“My brains are burning from waking up early, community service then studio….but I did the crime. I only have myself to blame,” she tweeted Friday morning.

The "Bodak Yellow" rapper was at the NYPD Police Academy on Friday.
The “Bodak Yellow” rapper was at the NYPD Police Academy on Friday.

A picture of Cardi B.
The rapper was there for an “event” with Chief of Training Juanita Holmes, the sources said.FilmMagic for Capture Studio Group
The “UP” superstar was dressed to the nines in a selfie that accompanied the tweet. She wore the same designer duds during her pitstop at the Police Academy: A pricey cropped beige bomber jacket over a black shirt, white pants, a black Chanel beanie, and too-cool tan heeled boots.

“You having a tabi boot fashion show at community service daily,” tweeted Jodi900@FeRaw.

The “Bodak Yellow” rapper, 30, was ordered to complete the 15 days of community service as part of the plea deal she reached in September following a 2018 fight at a strip club.

A picture of Cardi B.
Cardi B is currently doing court-ordered community service following a 2018 fight at a strip club.

A picture of Cardi B.
“My brains are burning from waking up early, community service then studio….but I did the crime. I only have myself to blame,” Cardi B tweeted Friday morning.FilmMagic for Capture Studio Group
The Grammy winner was supposed to complete her community service last month but the deadline was extended to March 1.

Cardi’s original deadline to complete the service was in January.