In 1511, Niccolò di Bernardo dei Machiavelli was in the service of Piero Soderini, the Prior of the Florence Republic, as an ambassador to Pope Julius II, and to Cesare Borgia as Secretary of the Florentine Chancellery.
In 1512, forces of the Florentine Republic were defeated by the Spanish army, and the Florentine government collapsed. With the blessing and military support of Pope Julius, the Medici family was returned to power in the city they had previously ruled, and Machiavelli was dismissed from his service to the Florentine Republic.
Machiavelli was implicated in a conspiracy against the Medici, and he was imprisoned, tortured, and threatened with execution. Machiavelli was found to have been wrongly accused, and he was released from prison, but he was exiled from Florence. He retired to his farm near the town of Sant’Andrea, about nine miles southwest of Florence, where he decided to pursue a writing career, and where he wrote The Prince between 1513 and 1514.
Machiavelli wrote The Prince for two reasons, one of which he explained in the dedication of the book, and the other which was implied by the nature of the book itself and the person to whom it was dedicated.
Machiavelli dedicated The Prince to Lorenzo Di Piero de' Medici, Duke of Urbino, and later governor of Florence, who was one of the most famous of the Medici at that time.
The book was intended to show a prince or other ruler of a city-state or country how best to maintain a safe, secure, and prosperous government in the midst of the seemingly never-ending political turmoil of early sixteenth-century Italy. As Machiavelli stated in the dedication, the book was based on Machiavelli's "knowledge of the deeds of great men which I have acquired through a long experience of modern events and a constant study of the past."
And although I deem this work unworthy of your countenance, nevertheless I trust much to your benignity that it may be acceptable to you, seeing that it is not possible for me to make a better gift than to offer you the opportunity of understanding in the shortest time all that I have learnt in so many years, and with so many troubles and dangers.
... I trust, therefore, that Your Highness will accept this little gift in the spirit in which it is offered ...
The "spirit in which" Machiavelli offered The Prince to Lorenzo de' Medici is both stated and implied.
On one hand, Machiavelli intended to enlighten Lorenzo and the Medici family with the book, "wherein, if it be diligently read and considered by you, you will learn my extreme desire that you should attain that greatness which fortune and your other attributes promise."
On the other hand, Machiavelli desperately wanted to leave his retirement and return to public life in Florence. He hoped to redeem himself in the eyes of the Medici, and he hoped that the knowledge and experience that he demonstrated in writing The Prince might prompt Lorenzo de' Medici to appoint him to an advisory position in the Medici government in Florence.
Although The Prince might well have enlightened Lorenzo and other Medici as to methods of government that they had not yet put into practice, Machiavelli was not invited to return to Florence as an adviser or in any other capacity, and he died in 1527, still living in exile in Sant’Andrea.