Shahjahanpur was established by Dilir Khan and Bahadar Khan, sons of Dariya Khan, a soldier in army of the Mughal emperor Jahangir. Dariya Khan was originally from Kandahar, in modern-day Afghanistan. Both Dilir Khan and Bahadar Khan were dignitaries in the regime of Shah Jahan. Pleased with the services of Dilir Khan, Shah Jahan gave him 17 villages with the permission to construct a fort in 1647, following the suppression of the rebellious Katheria Rajputs. The area was then settled by Afghans, brought by Bahadar Khan following one of his campaigns.
On 9 August 1925, the Indian freedom fighters Ram Prasad Bismil, Ashfaqulla Khan, Chandrashekhar Azad and Rajendra Lahiri conducted a robbery of government funds near Kakori railway station. Both Bismil and Khan were born in Shahjahanpur.