Powered By GitBook
Authenticating with a Token
Tokens are basically random strings, much like a password, and can provide a simple authentication mechanism in some situations. However, tokens are only as safe as they are secret so other authentication schemes can provide more security in large installations. It is highly recommended to use one of the other NATS authentication mechanisms.
For this example, start the server using:
1
> nats-server --auth mytoken
Copied!
The code uses localhost:4222 so that you can start the server on your machine to try them out.

Connecting with a Token

Go
Java
JavaScript
Python
Ruby
TypeScript
C
1
// Set a token
2
nc, err := nats.Connect("127.0.0.1", nats.Name("API Token Example"), nats.Token("mytoken"))
3
if err != nil {
4
log.Fatal(err)
5
}
6
defer nc.Close()
7
8
// Do something with the connection
Copied!
1
Options options = new Options.Builder().
2
server("nats://localhost:4222").
3
token("mytoken"). // Set a token
4
build();
5
Connection nc = Nats.connect(options);
6
7
// Do something with the connection
8
9
nc.close();
Copied!
1
let nc = NATS.connect({url: `nats://127.0.0.1:${port}`, token: "mytoken!"});
Copied!
1
nc = NATS()
2
3
await nc.connect(servers=["nats://demo.nats.io:4222"], token="mytoken")
4
5
# Do something with the connection.
Copied!
1
NATS.start(token: "mytoken") do |nc|
2
puts "Connected using token"
3
end
Copied!
1
let nc = await connect({url: server.nats, token: "mytoken"});
Copied!
1
natsConnection *conn = NULL;
2
natsOptions *opts = NULL;
3
natsStatus s = NATS_OK;
4
5
s = natsOptions_Create(&opts);
6
if (s == NATS_OK)
7
s = natsOptions_SetToken(opts, "mytoken");
8
if (s == NATS_OK)
9
s = natsConnection_Connect(&conn, opts);
10
11
(...)
12
13
// Destroy objects that were created
14
natsConnection_Destroy(conn);
15
natsOptions_Destroy(opts);
Copied!

Connecting with a Token in the URL

Some client libraries will allow you to pass the token as part of the server URL using the form:
nats://token@server:port
Again, once you construct this URL you can connect as if this was a normal URL.
Go
Java
JavaScript
Python
Ruby
TypeScript
C
1
// Token in URL
2
nc, err := nats.Connect("[email protected]")
3
if err != nil {
4
log.Fatal(err)
5
}
6
defer nc.Close()
7
8
// Do something with the connection
Copied!
1
Connection nc = Nats.connect("nats://[email protected]:4222");//Token in URL
2
3
// Do something with the connection
4
5
nc.close();
Copied!
1
let url = `nats://[email protected]:${port}`;
2
let nc = NATS.connect({url: url});
Copied!
1
nc = NATS()
2
3
await nc.connect(servers=["nats://[email protected]:4222"])
4
5
# Do something with the connection.
Copied!
1
NATS.start("[email protected]:4222") do |nc|
2
puts "Connected using token!"
3
end
Copied!
1
let url = `nats://:[email protected]:${port}`;
2
let nc = await connect({url: url});
Copied!
1
natsConnection *conn = NULL;
2
natsOptions *opts = NULL;
3
natsStatus s = NATS_OK;
4
5
s = natsOptions_Create(&opts);
6
if (s == NATS_OK)
7
s = natsOptions_SetURL(opts, "nats://[email protected]:4222");
8
if (s == NATS_OK)
9
s = natsConnection_Connect(&conn, opts);
10
11
(...)
12
13
// Destroy objects that were created
14
natsConnection_Destroy(conn);
15
natsOptions_Destroy(opts);
Copied!
Last modified 1yr ago