Just been through this with my Z4 key, only had one key, I bought key blanks from ebay and had them cut by a local locksmith so they worked the door, boot and ignition locks. I then used a AK90 coding machine also obtained from ebay and had a go at coding the new keys. You have to remove the EWS module from the car, its behind the side panel in the right hand drivers footwell, secured in there with a 10mm nut. Once out the internal circuit board has to be removed and using the AK90 coding machine read the processor chip on the board, the AK90 is connected to a laptop and the EWS software will download the Bin file which contains the cars details. It uses the Bin file to code the key. It involvesw writing the data in the Bin file to the small transponder chip in the key.
Try as I did I vcould not get a key successfully coded using the AK90, said key was ok but wouldn't start the car. Gave up in the end and sought out a mobile locksmith who used another coding machine and pulled the car Bin file using a different methos than the AK90, he pulled the key data from the eeprom on the circuit board and used that data to code my key. First try was to code it to key slot 8 which didn't crank the car, recoded to key slot 3 and I had a working key. Charged me £120, cheaper than getting one from BMW.
I also have BMW Scanner 1.4 which is a diagnostic software, with that you can explore all the modules on the car including the EWS system and can view what keys are coded to your car. In my case it listed three keys in use and indicated that they were all authorised for use with correct Vin number and password assigned to them. It listed key slots 4 to 10 as not authorised. Previous attempts at coding to these unauthorised slots failed to get a working key.
Spoke to quite a few people on here who have coded keys with an AK90 and have said how easy it was to do, proved very different when I tried and I know I was doing it right, came to the conclusion I had a bad AK90 machine.
IamOrion posted on here earlier, he was very helpful and had a look at what I was doing remotely on my laptop, if you are anywhere near him he may be able to code it for you as he uses the AK90 with good results.
My advice if your nowhere near him would be to find a mobile locksmith to come and code your key, saves a lot of trouble at the end of the day.