When the testimony of a material witness for the people is required in a criminal action before a court of record of this state, and such witness is a prisoner in the state prison or in a county jail, an order for his temporary removal from such prison or jail, and for his production before such court, may be made by the court in which the action is pending, or by a judge thereof; but in case the prison or jail is out of the county in which the application is made, such order can only be made upon the affidavit of the prosecuting attorney or other person on behalf of the people showing that the testimony is material and necessary; and even then the granting of the order is in the discretion of the court or judge. The order must be executed by the sheriff of the county in which it is made, whose duty it is to bring the prisoner before the proper court, to safely keep him, and when he is no longer required as a witness, to return him to the prison or jail whence he was taken. The expense of executing such order must be paid by the county in which the order is made.
Idaho Code § 19-3012