You have a few options, but the two main options I see depend on if you plan to actually fight this with a lawyer or if you have decent speaking skills and want to see if you can get around this yourself. If you go the lawyer route, go in there and when the judge calls you up, just say not guilty and follow their instructions from there. For the other option, this will kind of depend on the judge, some will require a guilty/not guilty plea where as others may let you speak with them. If you have to enter a plea, say not guilty and then speak with the prosecutor handling your case and if the judge has you speak to them then you can explain your case.
I would open up with an apology for your actions and explain you weren't thinking at the time when your so called friend asked and will never do it again. If the person you are talking to seems bent on doing something to you instead of letting you off, then ask if it would be possible to either go to some sort of remediation, like rehab or a training course, or some community service hours with the possibility of the record being dropped if you don't get into any more trouble.
Obviously dress nice and be all yes sir/ma'am to them while using proper grammar. Even if they are an ass to you, be nice.