Compartmentalization of cellular signaling forms the molecular basis of cellular behavior. The primary cilium constitutes a subcellular compartment that orchestrates signal transduction independent from the cell body. Ciliary dysfunction causes severe diseases, termed ciliopathies. Analyzing ciliary signaling has been challenging due to the lack of tools to investigate ciliary signaling. Here, we describe a nanobody-based targeting approach for optogenetic tools in mammalian cells and in vivo in zebrafish to specifically analyze ciliary signaling and function. Thereby, we overcome the loss of protein function observed after fusion to ciliary targeting sequences. We functionally localized modifiers of cAMP signaling, the photo-activated adenylyl cyclase bPAC and the light-activated phosphodiesterase LAPD, and the cAMP biosensor mlCNBD-FRET to the cilium. Using this approach, we studied the contribution of spatial cAMP signaling in controlling cilia length. Combining optogenetics with nanobody-based targeting will pave the way to the molecular understanding of ciliary function in health and disease.