"I've worked hard to return to full competitive form.
"But I also know my body's capacities and limits, and its message to me lately has been clear.
"I am 41 years old. I have played more than 1500 matches over 24 years. Tennis has treated me more generously than I ever would have dreamt, and now I must recognise when it is time to end my competitive career.
"The Laver Cup next week in London will be my final ATP event. I will play more tennis in the future, of course, but just not in grand slams or on the tour.
"This is a bittersweet decision, because I will miss everything the tour has given me. But at the same time, there is so much to celebrate. I consider myself one of the most fortunate people on Earth. I was given a special talent to play tennis, and I did it at a level that I never imagined, for much longer than I ever thought possible."
In the letter, Federer also thanked his family, in particular his wife, children, parents and sister, as well as tennis influences who helped him along the way, including Swiss Tennis, former coaches, sponsors and his team.
Federer also thanked his competitors.
"I was lucky enough to play so many epic matches that I will never forget," Federer continued.
"We battled fairly, with passion and intensity, and I always tried my best to respect the history of the game. I feel extremely grateful. We pushed each other, and together we took tennis to new levels.
"The last 24 years on tour have been an incredible adventure. While it sometimes feels like it went by in 24 hours, it has also been so deep and magical that it seems as if I've already lived a full lifetime.
"I have had the immense fortune to play in front of you in over 40 different countries. I have laughed and cried, felt joy and pain, and most of all I have felt incredibly alive. Through my travels, I have met many wonderful people who will remain friends for life, who consistently took time out of their busy schedules to come watch me play and cheer me on around the globe. Thank you.
"When my love of tennis started, I was a ball kid in my hometown of Basel. I used to watch the players with a sense of wonder. They were like giants to me and I began to dream. My dreams led me to work harder and I started to believe in myself. Some success brought me confidence and I was on my way to the most amazing journey that has led to this day.
"So, I want to thank you all from the bottom of my heart, to everyone around the world who has helped make the dreams of a young Swiss ball kid come true.
"Finally, to the game of tennis: I love you and will never leave you."
In July at Wimbledon, Federer revealed he hoped to go out with one more trip to the All-England Club, but a month later announced he would undergo yet another knee surgery which would see him sidelined "for many months".
He was also announced as part of the Big Four line up which included Nadal, Djokovic and Andy Murray for Team Europe at the tournament.
Between Wimbledon 2003 and Wimbledon 2022, the quartet claimed 66 of the 76 grand slam titles.
But at 41, it was considered a matter of time before the oldest of the stars ended his legendary run in the game.
Nadal praised his "friend and rival" in a tweet shortly after the news.
"I wish this day would have never come," said Rafa.
"It's a sad day for me personally and for sports around the world. It's been a pleasure but also an honour and privilege to share all these years with you, living so many amazing moments on and off the court."