Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A Ruby script to import Trello cards from a CSV file
#!/usr/bin/env ruby
# You can skip this bit if you wish - you will need the 'ruby-trello' gem installed, and
# optionally, 'foreman' to run the script with.
require 'bundler/setup'
require 'trello'
require 'csv'
# TrelloCardsFromCSV
# This class imports cards onto a Trello board from a CSV file file in the following format:
# - ID (can be anything, or blank - just a way of identifying a card
# - Name (name of card)
# - Description (description of card)
# This Ruby script is designed to be run using `foreman run trello-cards-from-csv.rb [filename]`, as it
# uses a number of environment variables. From a file named '.env'. This file should contain each of the variables below on a new line, but you'll only need to enter them once.
# Alternatively you can set up the variables you need before
# running the script, like so:
# TRELLO_APP_ID=xxx TRELLO_APP_SECRET=xxx TRELLO_USER_TOKEN=xxx BOARD_NAME="xxx" MEMBER_NAME="xxx" ./trello-cards-from-csv.rb
# Here's what each of these variables are:
# TRELLO_APP_ID, TRELLO_APP_SECRET - The Trello application details generated from:
# TRELLO_USER_TOKEN - The token you get by visiting:[YOUR TRELLO APP ID]&name=MyApp&response_type=token&scope=read,write,account&expiration=never in your browser and granting permissions.
# BOARD_NAME - the name of the board to add cards to (be sure to quote)
# MEMBER_NAME - the name of the member to authenticate as
# See methods below for more documentation.
class TrelloCardsFromCSV
# Include Trello classes so that we can use them directly
include Trello
include Trello::Authorization
attr_accessor :filename, :board_name, :member, :member_name, :board, :label
# Initialize the object and process the card file
# filename - the relative name of the file to read card data from (must be well-formed CSV)
# board_name - the name of the board to add cards to (comes from an environment variable)
# member_name - the name of the member to add cards as (comes from an environment variable)
# label - the label to apply to created cards (optional, but in this script defaults to 'blue')
def initialize(filename, board_name, member_name, label = nil)
self.filename = filename
self.board_name = board_name
self.member_name = member_name
self.label = label
# Set up the Trello authorization. This uses a number of environment variables
# directly to set up the authorization with Trello.
Trello::Authorization.const_set :AuthPolicy, OAuthPolicy
OAuthPolicy.consumer_credential = ENV['TRELLO_APP_ID'], ENV['TRELLO_APP_SECRET']
OAuthPolicy.token = ENV['TRELLO_USER_TOKEN'], nil
# Find the member to use when looking for boards
self.member = Member.find(self.member_name)
# Find the board by it's name
self.board = { |b| == self.board_name }.first
# Stop running the script if a matching board could not be found
raise "No board found" unless board
puts "Using board #{}"
# Assume first list is the one we want to import to
list_id =
puts "Using list #{}"
# Open file and import
CSV.foreach(filename) do |row|
# The card will be created with a name such as 001 User Authentication
# and a description which will be whatever is in the final column of the CSV
card = Card.create({
:list_id => list_id,
:name => row[0..1].join(" "),
:description => row[2]
# A card label is applied here if one is passed in - note that 'colors' must be used
# rather than any text applied to the label.
card.add_label(self.label) if self.label
puts "Imported #{}"
# Set up and run the script, passing in environment variables except for the filename
# , which is passed in from the command, and the label, which is fixed.

This comment has been minimized.

Copy link

@jmccartie jmccartie commented May 3, 2013

Thanks for this script, @joshmcarthur!

I ran into this error when trying to run it:

trello.rb:56: warning: already initialized constant AuthPolicy
Using board Development
Using list Next Up
[400 POST]: could not find the board that the card belongs to

/Users/jmccartie/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/ruby-trello-1.0.1/lib/trello/client.rb:98:in `invoke_verb': could not find the board that the card belongs to (Trello::Error)
    from /Users/jmccartie/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/ruby-trello-1.0.1/lib/trello/client.rb:23:in `post'
    from /Users/jmccartie/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/ruby-trello-1.0.1/lib/trello/card.rb:79:in `save'
    from /Users/jmccartie/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/ruby-trello-1.0.1/lib/trello/basic_data.rb:23:in `save'
    from /Users/jmccartie/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/ruby-trello-1.0.1/lib/trello/client.rb:66:in `create'
    from /Users/jmccartie/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/ruby-trello-1.0.1/lib/trello/card.rb:20:in `create'
    from trello.rb:88:in `block in initialize'
    from /Users/jmccartie/.rbenv/versions/1.9.3-p392/lib/ruby/1.9.1/csv.rb:1792:in `each'
    from /Users/jmccartie/.rbenv/versions/1.9.3-p392/lib/ruby/1.9.1/csv.rb:1208:in `block in foreach'
    from /Users/jmccartie/.rbenv/versions/1.9.3-p392/lib/ruby/1.9.1/csv.rb:1354:in `open'
    from /Users/jmccartie/.rbenv/versions/1.9.3-p392/lib/ruby/1.9.1/csv.rb:1207:in `foreach'
    from trello.rb:77:in `initialize'
    from trello.rb:109:in `new'
    from trello.rb:109:in `<main>'

Any ideas? Was there a particular version of the trello gem that you were using? Maybe it's changed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment