Etymonline says that this is a rather recent phrase, first recorded in 1919. The Phrase Finder supplies two slightly older quotes.
The earliest uses of 'spill the beans' come from the USA. The meaning of the phrase was then something like 'spoil the beans' or 'upset the applecart'[.] The first example I can find is from The Stevens Point Journal, June 1908 [.]
Soon after that the phrase was used with the meaning of 'upset a previously stable situation by talking out of turn', which is close to how we use it today. That is cited in The Van Wert Daily Bulletin, October 1911:
Finally Secretary Fisher, of the President's cabinet, who had just returned from a trip to Alaska, was called by Governor Stubbs to the front, and proceeded, as one writer says, to 'spill the beans'.
Note the quotation marks, which suggest that this wasn't an established phrase back then.