Your Magic Keyboard and Magic Mouse 2 each have a built-in rechargeable battery.
Charge the battery. Connect your Magic Keyboard or Magic Mouse 2 to your iMac with the included Lightning to USB cable. To check the charge level, click Apple Lithium-ion Batteries.