Python path for allowing animation of viewport and render visibility of objects
Keyframe Insert / Delete.
Further to the answer of @scurest

Mouse over the property after setting to refresh the panel
Every object in blender has a keyframe_insert and keyframe_delete method.   It does everything required, eg if the object has no animation data or action it adds them, then adds the keyframe.
Set the value, insert the keyframe. eg hide the object at frame 10
&gt;&gt;&gt; C.object.hide_viewport = True
&gt;&gt;&gt; C.object.keyframe_insert('hide_viewport', frame=10)
True

Can pass the frame, or it defaults to the current frame of the scene.