From healthline:
The most obvious cause of foamy urine is the speed of urination. Just as water foams up when it comes out of the tap quickly, urine foams if it hits the toilet quickly. This kind of foam should also clear up quickly.
Sometimes, urine can also foam up when it’s concentrated. Your urine is more concentrated if you haven’t had much water to drink and you’re dehydrated.
Foamy urine can also indicate that you have too much of a protein, such as albumin, in your urine. The protein in your urine reacts with the air to create foam.
Normally, your kidneys filter extra water and waste products out of your blood into your urine. Protein and other important substances that your body needs are too big to fit through the kidneys’ filters, so they stay in your bloodstream.
But when your kidneys are damaged, they don’t filter as well as they should. Damaged kidneys can allow too much protein to leak into your urine. This is called proteinuria. It’s a sign of chronic kidney disease or the late stage of kidney damage, called end-stage renal disease.
A less common cause of foamy urine is retrograde ejaculation, which is a condition that happens in men when semen backs up into the bladder instead of being released from the penis.
Amyloidosis is a rare condition that can also cause foamy urine, fluid buildup, and problems for the kidneys. It is caused by the buildup of a specific protein, and can affect many organs.
Taking the medicine phenazopyridine (Pyridium, AZO Standard, Uristat, AZO) is another less common cause of foamy urine. People take this medication to treat the pain from urinary tract infections.
And sometimes, the problem is actually just your toilet. Some toilet cleaning chemicals can make your urine look foamy. If this is the cause, the foam should stop as soon as you flush the cleaner out of the toilet.