<p>You can either use your own bitcoind server or you can use a 3rd party API service.</p>

<p>Let's do a few examples of what you are asking.<br>
I'll use short fake addresses and private keys as examples to make things easy.    </p>

<h2>PHP with a bitcoind server</h2>

<pre><code>&lt;?php
require("easybitcoin.php");
$bitcoin = new Bitcoin("username", "somepassword");

//add the private key (label, key)
$import = bitcoin-&gt;importprivkey("5KL3cN..SOME_PRIVATE_KEY", "Satoshi1");
?&gt;    
</code></pre>

<p>What we did here was import a private key and give it a specific unique label.  This part might take a while as your node will need to rescan as you added a new private key and it needs to check for transactions. </p>

<p>Once the node is rescanned you can send the funds.</p>

<pre><code>&lt;?
require("easybitcoin.php");
$bitcoin = new Bitcoin("username", "somepassword");

//send the funds from the Satoshi1 account/label
$send = $bitcoin-&gt;$bitcoin-&gt;sendfrom("Satoshi1","1receivingAddress", 0.5);
echo $send ? $send : "Oops an error: ".$bitcoin-&gt;error;
?&gt;    
</code></pre>

<p>What we did above was use the <code>sendfrom()</code> JSON RPC command to send 0.5BTC from the account/label Satoshi1 to the 1receivingAddress. If successful it will respond with the transaction ID, or if an error it will print out the error. </p>

<p>This is just the code, you'll still need to set your VPS with bitcoind or you can run a node on your home computer and experiment locally using something like xampp. </p>

<h2>PHP with a 3rd party API</h2>

<p>Here's an example with blockchain.info wallet API. You can import the private key using their create wallet utility, and then later send from that specific address.</p>

<pre><code>$private = "5KL3cN..SOME_PRIVATE_KEY";
$url = "http://127.0.0.1:3030/merchant/YOUR-GUID/create?password=YOUR-PASSWORD&amp;api_code=YOUR-API-CODE&amp;priv=".$private;
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //change to true if your host can verify SSL
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);        
curl_setopt($ch, CURLOPT_URL, $url);
$ccc = curl_exec($ch);
$json = json_decode($ccc, true);
echo "&lt;pre&gt;";
var_dump($json);
echo "&lt;/pre&gt;";
</code></pre>

<p>Then once created you can send from that specific new address that was imported. </p>

<pre><code>$from = "1fromAddress"; 
$to = "1receivingAddress";
$url = "http://127.0.0.1:3030/merchant/YOUR-GUID/payment?password=YOUR-PASSWORD&amp;api_code=YOUR-API-CODE&amp;from=".$from."&amp;amount=1000000&amp;to=".$to;
//amounts are in satoshi so 1000000 is 0.01BTC
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);        
curl_setopt($ch, CURLOPT_URL, $url);
$ccc = curl_exec($ch);
$json = json_decode($ccc, true);
echo "&lt;pre&gt;";
var_dump($json);
echo "&lt;/pre&gt;";
</code></pre>

<p>If you need help setting up bitcoind or blockchain.info's wallet API I suggest you check out my tutorials on youtube. The hardest part with the 3rd party route is getting an API key; I hear blockchain.info has been strict with these lately.   </p>

<p>bitcoind server method: <a href="https://www.youtube.com/watch?v=cMM-t7azzJE" rel="nofollow">https://www.youtube.com/watch?v=cMM-t7azzJE</a><br>
blockchain.info 3rd party method: <a href="https://www.youtube.com/watch?v=X8jsaf4sEgs" rel="nofollow">https://www.youtube.com/watch?v=X8jsaf4sEgs</a></p>

CC BY-SA 3.0
2016-09-25T22:33:05.897Z
im assuming this is the version i would need to download and run? https://bitcoin.org/en/download -- i just want to make sure. thanks for the answer!
Patoshi パトシ
CC BY-SA 3.0
2016-09-25T22:34:26.457Z
Also can this be done with other wallets that doesnt require downloading the entire blockchain like bitcoin core?
Patoshi パトシ
CC BY-SA 3.0
2016-09-26T02:12:11.327Z
The bitcoind version would require you to download and run bitcoin core. You can prune it though, if you are concerned with hard-drive space and still have full wallet functionality.
m1xolyd1an
CC BY-SA 3.0
2016-10-11T19:50:48.343Z
but the thing is i dont want it to store the private keys. is there a service for such a thing? or I would just have to remove it from these apps manually? What command would one use then?
Patoshi パトシ
CC BY-SA 3.0
2016-09-24T23:37:35.290Z
2016-09-24T23:37:35.290Z
m1xolyd1an
<p>I'm building an app and one of my features is being able to scan a bitcoin private key and then send a portion of its funds to another address. How can I do something like this in javascript or php? Do I need to run my own Bitcoin server? </p>

<p>Is there an example script I can use where I can just plug in a private key?</p>

<private-key><php><sending><javascript>
How to send portion of funds from a private key programmatically?
Patoshi パトシ
# How to send portion of funds from a private key programmatically?

(Asked by: username_0 on 2016-09-24)

I'm building an app and one of my features is being able to scan a bitcoin private key and then send a portion of its funds to another address. How can I do something like this in javascript or php? Do I need to run my own Bitcoin server?

Is there an example script I can use where I can just plug in a private key?

**Tags:** private-key, php, sending, javascript


---

## 1 answers

### Answer by username_1 on 2016-09-24. Score: 2

You can either use your own bitcoind server or you can use a 3rd party API service.

Let's do a few examples of what you are asking.  
I'll use short fake addresses and private keys as examples to make things easy.

## PHP with a bitcoind server

```
<?php
require("easybitcoin.php");
$bitcoin = new Bitcoin("username", "somepassword");

//add the private key (label, key)
$import = bitcoin->importprivkey("5KL3cN..SOME_PRIVATE_KEY", "Satoshi1");
?>    

```

What we did here was import a private key and give it a specific unique label. This part might take a while as your node will need to rescan as you added a new private key and it needs to check for transactions.

Once the node is rescanned you can send the funds.

```
<?
require("easybitcoin.php");
$bitcoin = new Bitcoin("username", "somepassword");

//send the funds from the Satoshi1 account/label
$send = $bitcoin->$bitcoin->sendfrom("Satoshi1","1receivingAddress", 0.5);
echo $send ? $send : "Oops an error: ".$bitcoin->error;
?>    

```

What we did above was use the `sendfrom()` JSON RPC command to send 0.5BTC from the account/label Satoshi1 to the 1receivingAddress. If successful it will respond with the transaction ID, or if an error it will print out the error.

This is just the code, you'll still need to set your VPS with bitcoind or you can run a node on your home computer and experiment locally using something like xampp.

## PHP with a 3rd party API

Here's an example with blockchain.info wallet API. You can import the private key using their create wallet utility, and then later send from that specific address.

```
$private = "5KL3cN..SOME_PRIVATE_KEY";
$url = "http://127.0.0.1:3030/merchant/YOUR-GUID/create?password=YOUR-PASSWORD&api_code=YOUR-API-CODE&priv=".$private;
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //change to true if your host can verify SSL
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);        
curl_setopt($ch, CURLOPT_URL, $url);
$ccc = curl_exec($ch);
$json = json_decode($ccc, true);
echo "<pre>";
var_dump($json);
echo "</pre>";

```

Then once created you can send from that specific new address that was imported.

```
$from = "1fromAddress"; 
$to = "1receivingAddress";
$url = "http://127.0.0.1:3030/merchant/YOUR-GUID/payment?password=YOUR-PASSWORD&api_code=YOUR-API-CODE&from=".$from."&amount=1000000&to=".$to;
//amounts are in satoshi so 1000000 is 0.01BTC
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);        
curl_setopt($ch, CURLOPT_URL, $url);
$ccc = curl_exec($ch);
$json = json_decode($ccc, true);
echo "<pre>";
var_dump($json);
echo "</pre>";

```

If you need help setting up bitcoind or blockchain.info's wallet API I suggest you check out my tutorials on youtube. The hardest part with the 3rd party route is getting an API key; I hear blockchain.info has been strict with these lately.

bitcoind server method: <https://www.youtube.com/watch?v=cMM-t7azzJE>  
blockchain.info 3rd party method: <https://www.youtube.com/watch?v=X8jsaf4sEgs>

> **Comments:**
> **username_0**: im assuming this is the version i would need to download and run? https://bitcoin.org/en/download -- i just want to make sure. thanks for the answer!  
> **username_0**: Also can this be done with other wallets that doesnt require downloading the entire blockchain like bitcoin core?  
> **username_1**: The bitcoind version would require you to download and run bitcoin core. You can prune it though, if you are concerned with hard-drive space and still have full wallet functionality.  
> **username_0**: but the thing is i dont want it to store the private keys. is there a service for such a thing? or I would just have to remove it from these apps manually? What command would one use then?  

---

