When was the earliest recorded battle (on script) with the usage of gunpowder (in any form) as an offensive weapon?
The first recorded battle using what is believed to be gunpowder is the siege of Di'an in 1132. Although the weapons are not guns, it is well attested that the Chinese at this time had perfected gunpowder, so it can be assumed that this is what is being used.
Guns appear in China during the 12th and 13th century, the oldest preserved one is from around 1288. It can in general be assumed that they were used during all Chinese battles at this time, although no records specifically mentions them, as they weren't news and hence not worthy of mention.
However, when the Mongols used catapult launched bombs at the battle of Mohi it was certainly news to the Hungarians, and as such several sources report this. It's therefore often cited as the first mention of gunpowder in a battle.
Various claims have been made by alleged inventors and the Germans without any foundation claimed to have invented it in the early part of the 14th Century. Written evidence exists however, that the English monk, Roger Bacon, wrote about gunpowder in 1267. Even this was late as there is no doubt that Arabs used it at the siege of Mecca in 690AD.
There are some grounds for believing that gunpowder was known to the Hindu Indians centuries before the siege of Mecca and this theory is borne by the following extract from a law code known as the Gentoo Laws, written about 1300 B.C.
"THE MAGISTRATE WILL NOT MAKE WAR WITH CANNON OR WITH ANY KIND OF FIREARM”
It should be noted that the words "cannon" and "firearm" would appear to have the same meaning. This text proves that Indians did in fact use some kind of propellant powder and in all probability was of similar composition of the present day, black powder. The Gentoo Laws lend some support to this claim as far as gunpowder is concerned. Wherever it was first used, gunpowder certainly was not invented by any of the Western races, as all evidence suggests it originated in the East and travelled westwards
If you consider Greek fire as a precursor of gunpowder, then the first use if around 671 AD by the Byzantine fleet in Constantinople. Anyway, my answer is to point to the book by JR Partington whose title is "A history of Greek fire and gunpowder".
This book contains a lot of information about the genesis of gunpowder.
I believe it was 690 by the Arabs on the siege of Mecca. Source: source: Chemical Rocket Propulsion by Luigi De Luca et al
The first unequivocal and provable use of gunpowder in any significant way during a battle which involved an actual gun (cannon throwing shot) was at the Battle of Crecy (1346). Even in this case, the account (by Froissart) can be questioned as possibly exaggerated or anachronistic. The first absolutely provable use of gunpowder by reference to actual contemporary, verifiable documents occurred at various sieges in France which occurred in the 1370s. In those cases there are actual letters and inventories showing the purchase of guns and gunpowder and are factually incontrovertible.
All the mentions identified by later historians speculating on supposed uses of gunpowder prior to 1346 are even more dubious (in most cases much more dubious) than Froissart's description.
Just to address one such dubious claim is the reference to the Battle of Mohi. In this case a Chinese encyclopedia written hundreds of years after the actual battle claimed that "fire pots" were used in the battle. Some so-called scholars have inferred from this account that the Mongols used gunpowder, a completely ridiculous idea. The European accounts of the battle make no mention of fire pots, probably because the use of incendiaries was commonplace in Europe at the time and had been commonplace for over 1000 years. An incendiary is not a gunpowder, it is any flammable substance, which includes naptha, pitch, oil and many other such substances.