The expression "riding shotgun" is derived from"shotgun messenger" , a colloquial term for "express messenger", in the days of stagecoach travel the person in the position next to the driver.
However, apparently the phrase "riding shotgun" was not coined until 1919
It was later used in print and especially film depiction of stagecoaches and wagons in the Old West in danger of being robbed or attacked by bandits. A special armed employee of the express service using the stage for transportation of bullion or cash would sit beside the driver, carrying a short shotgun (or alternatively a rifle to provide an armed response in case of threat to the cargo, which was usually a strongbox.
Absence of an armed person in that position often signaled that the stage was not carrying a strongbox, but only passengers