Create a libp2p Node


In this tutorial, you will use a boilerplate Go project to set up two libp2p nodes and connect them, so you can begin to explore the capabilities of libp2p, such as data transmission, peer identification, routing, messaging, content discovery, and more.

By the end of the exercise, you should be able to:

  • Create a new libp2p node
  • Print the node’s ID and multiaddresses
  • Configure the node at a basic level
  • Connect to another node

Application diagram


  • You must have Go installed. In this exercise, version 1.18 is used.
  • Clone or fork the Git repository, which contains all the sample applications used in the Launchpad program.


  • Open the /libp2p-go-simple-code folder of the launchpad-tutorials repository in an IDE of your preference. The app subfolder contains the template that you will complete.


This video will demonstrate how you can create simple libp2p node in a Go application.

Review the “main” Function

In the main.go file, review the code, at about line 40. There are several functions that are called which you will implement in this tutorial. The main() function manages the flow of the program by calling different helper functions.

Create the Source Node

  • In the createSourceNode function, create a libp2p node by using the libp2p.New() function. This method returns a host.Host interface, which you can use to manage the node.
func createSourceNode() host.Host {
	node, err := libp2p.New()
	if err != nil {

	return node

By default, the node gets an ID and listens at a random TCP port.

Create the Target Node

  • Now, in the createTargetNode function, create a new node that listens at the 8007 TCP port. You can configure a node by passing several Option structs to the New(...) method.
func createTargetNode() host.Host {
	node, err := libp2p.New(
	if err != nil {

	return node

Connect the Nodes

  • So far, you have created two nodes; now, let’s connect sourceNode to targetNode. The host.Host interface contains a Connect method that you can use. The Connect method expects a peer.AddrInfo struct, which is an abstraction that represents the location of a peer. To create a peer.AddrInfo struct with the data of the node, you can use the host.InfoFromHost function.
func connectToTargetNode(sourceNode host.Host, targetNode host.Host) {
	targetNodeAddressInfo := host.InfoFromHost(targetNode)

	err := sourceNode.Connect(context.Background(), *targetNodeAddressInfo)
	if err != nil {

Count the Number of Peers of the Source Node

  • To verify that the connection works, you can list the peers connected to the node. In this example, you will simply count the number of peers and return that number to verify that your libp2p node is connected to peers.
func countSourceNodePeers(sourceNode host.Host) int {
	return len(sourceNode.Network().Peers())

Run the Program

  • Now, test that the two nodes are connected by running the application. In your terminal, cd to the /libp2p-go-simple-node/app directory and run go run .

You should see the following output, with the number of source node peers:

> go run .
ID: 12D3KooWCGcgrrrfDwzLmNeZ25543kYcewKxXzgDkGJGNXw1ZUf3
Multiaddresses: /ip4/, /ip4/, /ip6/::1/tcp/63681
ID: 12D3KooWLkzhtJxcSnasfzkXGQzgKGqxGtUDpACZSXt3HM4Rn3op
Multiaddresses: /ip4/, /ip4/
Source node peers: 1

You can see an example of the completed code here

Now that you have two nodes connected and communicating, you can start to implement the many features available with libp2p. In a later tutorial, you will learn how to start a libp2p stream.