Fentanyl is a Schedule II controlled substance. So while yes, it’s “legal” - it’s an extremely controlled narcotic.
Said differently, it’s illegal if you’re in position of it and it wasn’t prescribed by a medical doctor.
Unlike marijuana that doesn’t need a prescription where it’s allowed.