How can I create a keyframe with a script?

Basically I have a rig and I want to use a script to take the rig and make a Keyframe of its current position, its body part positions, etc. Not sure how to do that

You should look at the Keyframe API, which, in addition to explaining how Keyframes work, provides an example script that generates Keyframes for a rig.