Trying to start here;
https://www.stellar.org/developers/guides/get-started/create-account.html

Executing this code;

var request = require('request');
console.log("Create account");
request.get({
url: 'https://horizon-testnet.stellar.org/friendbot',
qs: { addr: publicKey },
json: true
}, function(error, response, body) {
console.log("Create account responded");

if (error || response.statusCode !== 200) {
console.error('ERROR!', error || body);
}
else {
console.log('SUCCESS! You have a new account ?\n', body);
}
});

I get;

Create account responded
ERROR! { type: 'https://stellar.org/horizon-errors/transaction_failed',
title: 'Transaction Failed',
status: 400,
detail: 'The transaction failed when submitted to the stellar network. The ex
tras.result_codes
field on this response contains further details. Description
s of each code can be found at: https://www.stellar.org/developers/learn/concept
s/list-of-operations.html',
instance: 'horizon-testnet-002/VRoRpxkVLt-105555',
extras:
{ envelope_xdr: 'AAAAAGXNhLrhGtltTwCpmqlarh7s1DB2hIkbP//jgzn4Fos/AAAAZAAACT0A
AJgmAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAMJ3LZEovKG4qg68LSSJrymqcXI6hID2Jtnp+wIufnZsA
AAAXSHboAAAAAAAAAAAB+BaLPwAAAEBAF1NpbU4VAH8549n7QkDOe7sRRV0JA35lERX85QlsXmmjG+zX
dnsm5khLDPEzXZNLBLVnOWpvkGvtYolfcuUI',
result_codes: { transaction: 'tx_bad_seq' },
result_xdr: 'AAAAAAAAAAD////7AAAAAA==' } }

Any suggestions? I couldn't find TX_BAD_SEQ at https://www.stellar.org/developers/learn/concepts/list-of-operations.html

EDIT:
Found a different set of instructions here https://github.com/stellar/js-stellar-sdk/blob/master/docs/reference/api/friendbot.md

So I tried
var StellarSdk = require('stellar-sdk');
var server = new StellarSdk.Server('https://horizon-testnet.stellar.org');
var pair = StellarSdk.Keypair.random();
var new_address = pair.accountId()
server.friendbot(new_address)
.then(function(resp) {
console.log(resp);
})
.catch(function(err) {
console.log(err);
});

It failed with

var new_address = pair.accountId()
^

TypeError: pair.accountId is not a function

    Try pair.publicKey in the second snippet, like you did in the first, instead of pair.accountId. The property names must have changed.

    Have you tried running the first snippet multiple times? Maybe friendbot has a racing condition problem when trying to fund your account and trying again may work (remember that friendbot is just a helper for creating accounts on the testnet and doesn't exist on mainnet).

    If eveything fails, try using the same address on the laboratory: https://www.stellar.org/laboratory/#account-creator?network=test -- fund your account there than go back to the tutorial.

      Thanks fiatjaf it now fails on

      server.friendbot(publicKey)
      .then(function(resp) {
      console.log(resp);
      })
      .catch(function(err) {
      console.log(err);
      });

      .then(function(resp) {
      ^
      TypeError: server.friendbot(...).then is not a function

      CarbonMan I'm using

      const request = require('request-promise');
      const StellarSdk = require('stellar-sdk');
      const pair = StellarSdk.Keypair.random();
      console.log(pair.secret(), pair.publicKey());
      
      request({
        url: 'https://horizon-testnet.stellar.org/friendbot',
        qs: { addr: pair.publicKey() },
        json: true
      })
      .then(function() {
        return self.getBalance(pair.publicKey());
      });

        umbrel Thanks
        It looks like the documentation at https://www.stellar.org/developers/guides/get-started/create-account.html could do with a little tweak. Changing to request-promise worked.
        Even though it appears to work, I still do get a funny message. I'll find out in time whether it really was successful.

        Create account
        Unhandled rejection NotFoundError: [object Object]

        Create account responded
        SUCCESS! You have a new account ?

        Oh, sorry, I just realized that part of my code doesn't make sense, because it uses another internal function.

        Full working example now:

        const request = require('request-promise');
        const StellarSdk = require('stellar-sdk');
        const pair = StellarSdk.Keypair.random();
        console.log(pair.secret(), pair.publicKey());
        
        request({
          url: 'https://horizon-testnet.stellar.org/friendbot',
          qs: { addr: pair.publicKey() },
          json: true
        })
        .then(function() {
          const server = new StellarSdk.Server('https://horizon-testnet.stellar.org');
          server.loadAccount(pair.publicKey())
            .then(function(account) { console.log(account); });
        });

        If it gives you bad_seq error, just try again, it's normal for friendbot to fail sometimes

          15 days later

          I tried for hours, but could never get request, request-promise or request-promise-native to work.

          I changed the code to use $.get() and it worked flawlessly. I'm on Windows 10, if that matters. Anyone else having problems should try $.get()

            a month later

            I had the same problem as CarbonMan. My solution was to just try the request again, it eventually worked!

            I'm just playing around with the JS SDK in Chrome, all that call is doing is a simple http request, so anyone that is getting a Uncaught ReferenceError: require is not defined , can simply just go to the url with the addr query parameter

            EX:
            https://horizon-testnet.stellar.org/friendbot?addr=GAE62FDQJCBBXFAGWDKIQKF6USEANBIXL4JJTMGKG5PYFXW3TADAX7HA

            3 months later

            CarbonMan Hi,friend!Could you show me your whole code about the creating test account?
            I am stucked for several days!Thanks!

            umbrel Hi,friend!Why am I still failed to create the account with your code???
            Have You tried it yourself?

            lhfly5201314 If I'm not wrong, $.get is part of the jquery library for javascript, you can use this library in javascript project to make requests to a server.

            There is a problem in friendbot, I am not able to fund a new public key. However, stellar laboratory version works.