user20712: This might be a possible duplicate with this.
I want to know a proper way to insert additional string with OP_RETURN and retrieve the transaction from block chain and decode it accordingly.
Thank you

user27169: You might find our OP_RETURN libraries useful, for PHP and Python. If you look at the code you can also see exactly how they work.

user2306: How do I add metadata to the blockchain?
Essentially, one of the output scripts should be
OP_RETURN <the data you want to add>

That's it.
Let me give an example. Say you want to add the string "Melons." to the blockchain.

Convert to binary
"Melons." -> 4d656c6f6e732e
Add the push-data-onto-stack command at the start. This is the length of your metadata if it's less than 75 bytes, or OP_PUSHDATAx otherwise.
074d656c6f6e732e
Add OP_RETURN at the start
6a074d656c6f6e732e
Put this in the scriptPubKey of an output of a transaction. I wrote a guide on this here.

How do I find it again?
That's... trickier. Bitcoin is not optimized for searching the metadata of transactions.
Possibilities:

Look through all of the transactions you get with something like bitcointools.
Use a website like coinsecrets.org.


20712: <p>This might be a possible duplicate with <a href="https://bitcoin.stackexchange.com/questions/25224/what-is-a-step-by-step-way-to-insert-data-in-op-return" title="What is a step by step way to insert data in OP_RETURN?">this</a>.</p>

<p>I want to know a proper way to insert additional string with OP_RETURN and retrieve the transaction from block chain and decode it accordingly.</p>

<p>Thank you</p>
 27169: <p>You might find our OP_RETURN libraries useful, for <a href="https://github.com/coinspark/php-OP_RETURN" rel="nofollow">PHP</a> and <a href="https://github.com/coinspark/python-OP_RETURN" rel="nofollow">Python</a>. If you look at the code you can also see exactly how they work.</p>
 2306: <h3>How do I add metadata to the blockchain?</h3>
<p>Essentially, one of the output <a href="https://en.bitcoin.it/wiki/Script" rel="nofollow noreferrer">scripts</a> should be</p>
<pre><code>OP_RETURN &lt;the data you want to add&gt;
</code></pre>
<p>That's it.</p>
<p>Let me give an example. Say you want to add the string &quot;Melons.&quot; to the blockchain.</p>
<ol>
<li>Convert to binary<br />
&quot;Melons.&quot; -&gt; <code>4d656c6f6e732e</code></li>
<li>Add the push-data-onto-stack command at the start. This is the length of your metadata if it's less than 75 bytes, or OP_PUSHDATAx otherwise.<br />
<code>074d656c6f6e732e</code></li>
<li>Add OP_RETURN at the start<br />
<code>6a074d656c6f6e732e</code></li>
<li>Put this in the scriptPubKey of an output of a transaction. I wrote a guide on this <a href="https://bitcoin.stackexchange.com/a/36439/2306">here</a>.</li>
</ol>
<h3>How do I find it again?</h3>
<p>That's... trickier. Bitcoin is not optimized for searching the metadata of transactions.</p>
<p>Possibilities:</p>
<ul>
<li>Look through all of the transactions you get with something like <code>bitcointools</code>.</li>
<li>Use a website like <a href="http://coinsecrets.org" rel="nofollow noreferrer">coinsecrets.org</a>.</li>
</ul>
