def get_fee(tx):
    """ Takes tx object and returns the transaction fee in satoshis """
    outputs = [output['value'] for output in tx['out']]
    try:
        inputs = [input['prev_out']['value'] for input in tx['inputs']]
    except KeyError:
        # This happens when there's a coinbase transaction
        inputs = []
    return sum(inputs) - sum(outputs)