Manage tags on an event
There are many tags you can use on Nostr events. Please check this overview with standardized tags to see which ones you can use on different event kinds. Please be aware that a tag can have a different meaning with different event kinds (e.g. an 'r' tag may have a meaning in an event of kind 1
and an entirely different meaning in an event of kind 10002
). You're not tied to this list, as you have the freedom to add any tag to an event. When doing this, expect that not all relays will accept your event.
Structure
Tags on a Event
class are formatted as an array. This array contains one or more arrays. Each array contains comma seperated values. Here is an example:
{
'id': '',
'pubkey': '',
'kind': 1,
'tags': [
['e', '<hex formatted event id>', '<relay url, optional>'],
['t', 'hashtag'],
['d', 'identifier'],
['client', 'Client tag / name', '31990:app1-pubkey:<d-identifier>', 'wss://relay1'],
['p', '<hex formatted pubkey>', '<relay url, optional>'],
['r', 'wss://relay1'],
['r', 'wss://relay2'],
...
],
'created_at': <unix timestamp>,
}
Adding tags
Tags are set on a Event
class with the addTag()
and setTags()
methods.
Add a single tags to an event:
$event = new Event();
$tag = ['t', 'php']; // Hashtag php
$event->addTag($tag);
$tag = ['r', 'wss://relay1'];
$event->addTag($tag);
Add multiple tags to an event:
$event = new Event();
$tags = [
['p', '<hex formatted pubkey>', '<relay url, optional>'],
['t', 'introductions'],
['r', 'wss://relay1'],
['r', 'wss://relay2'],
];
$event->setTags($tags);
Getting tags
Getting tags from an Event
is possible with the getTags
method.
$tags = $event->getTags();
This will return an array with all the tags.