How can I import contacts in CiviCRM using the command line (CLI) rather than using the CiviCRM user interface and its importing wizard?

2 Answers 2


When you are importing a large number of contacts, you could face timeout issues when using the user interface. Importing through CLI is an alternative.

There is a file called import.php in /civicrm/civicrm/bin/csv/import.php. We will call this file from the command line.

After you have exported your CSV file, here are the steps for the import:

  1. Put the CSV file on your CiviCRM server, outside the webroot somewhere. (Do not place this file within the website root, or you risk exposing your contacts' data to the internet.)
  2. In the command prompt (shell), type php path/to/import.php -e Contact --file path/to/file.csv (check this forum post for some more detail). Provide an absolute path to the CSV file.
  3. At this point, depending on the contents of your CSV file, you will likely get an error such as ERROR line 2: Mandatory key(s) missing from params array: contact_type. It expects you to pass a contact_type parameter inside the CSV file. If you don't have one in your exported CSV file, you can very easily add a contact_type column in it using a Notepad++ macro (note: turn off autocompletion for words and quotes in Notepadd++ as it may screw up the macro). Just add the new column at the end. Here is an example of a CSV file:

    "[email protected]","Individual"
    "[email protected]","Individual"

  4. Now run the command again and you should see a success message for each imported contact. The message looks like this: line 2: created Contact id: 114

The down side is that you cannot (yet) assign a group or a tag to the imported contacts. I looked into the API but could not see an ability to add contacts to groups or tags.


  1. If you get an error such as "cli.php can only be run from command line.", and you wonder about it because you ARE running from the command line, then likely you are using php as cgi/fcgi. To overcome this there is a little hack (which I am not too happy with, but we actually are using the code as intended here). In /bin/cli.class.php, on line 85 the function _accessing_from_cli() checks whether the program is run from CLI. It does this by checking the contents of PHP_SAPI. But according to the first comment here, that variable is not reliable in some cases. So we can modify the function like this:

public function _accessing_from_cli() { if (PHP_SAPI === 'cli') { return TRUE; } else { /* Temporary hack since the PHP_SAPI check does not work correctly for cgi/fcgi. See first comment here: http://php.net/manual/en/function.php-sapi-name.php */ return TRUE; //trigger_error("cli.php can only be run from command line.", E_USER_ERROR); } }

After you import your contacts, do not forget to return the file back to its original version.

  1. I really suggest you either configure php to show up errors or make sure you have access to the php error log. This way, if the import suddenly stops after some time, you'll be able to see the reason and take appropriate measures (increase memory_limit, max_execution_time, etc.).

Another option is to use drush in combination with the combination with the cvapi command. For example

drush cvapi Contact.create sequential=1 contact_type="Individual" first_name="Ben" middle_name="Lee" last_name="User"

creates one contact. Specially useful when sometimes one contact must created by automatically by an unix script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.