If i accidentally format my mac, do Apple offer to restore the OS through online?
*note: I dont have backup drives yet and i ddnt back up my mac for now.
You can't restore your files from Apple, but you can reinstall OS X from Apple.
my important question is that if i can get the mac os through apple online or should i buy it again?
As I told you, your Mac has got OS X Recovery, with Recovery HD (on your hard disk) and Internet Recovery (if you erased the whole hard drive or you want to reinstall the original OS X). You can use both to reinstall OS X.
In order to start into Recovery, press Command and R keys while your Mac is starting, so you will see "OS X Utilities" after starting. Here you can restore backups, reinstall OS X, browse the web, reset passwords or repair disks, and more.
To reinstall OS X, your Mac will download it from Apple, so it may take a long time to download