Module: NOWPayments::API::Authentication

Included in:
Client
Defined in:
lib/nowpayments/api/authentication.rb

Overview

JWT authentication endpoints

Instance Method Summary collapse

Instance Method Details

#authenticate(email:, password:) ⇒ Hash

Note:

Email and password are case-sensitive. [email protected] != [email protected]

Authenticate and obtain JWT token POST /v1/auth JWT tokens expire in 5 minutes for security reasons

Parameters:

  • email (String)

    Your NOWPayments dashboard email (case-sensitive)

  • password (String)

    Your NOWPayments dashboard password (case-sensitive)

Returns:

  • (Hash)

    Authentication response with JWT token

Raises:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/nowpayments/api/authentication.rb', line 15

def authenticate(email:, password:)
  response = post("auth", body: {
                    email: email,
                    password: password
                  })

  # Check for authentication errors
  if response.body.is_a?(Hash)
    # Handle 403 ACCESS_DENIED error
    if response.body["statusCode"] == 403 || response.body["code"] == "ACCESS_DENIED"
      error_msg = "Authentication failed: #{response.body["message"] || "Access denied"}. "
      
      if sandbox
        error_msg += "You are using SANDBOX mode. "
        error_msg += "Please verify:\n"
        error_msg += "   1. You have a sandbox account at https://sandbox.nowpayments.io/\n"
        error_msg += "   2. Your email and password are correct (case-sensitive)\n"
        error_msg += "   3. Your sandbox account has API access enabled"
      else
        error_msg += "You are using PRODUCTION mode. "
        error_msg += "Please verify:\n"
        error_msg += "   1. Your NOWPayments account at https://nowpayments.io/ has API access enabled\n"
        error_msg += "   2. Go to Settings → API → Enable API access if not already enabled\n"
        error_msg += "   3. Your email and password are correct (case-sensitive)\n"
        error_msg += "   4. If testing, you may need to use sandbox: true and sandbox credentials"
      end
      
      raise AuthenticationError.new(
        status: response.body["statusCode"],
        body: { "message" => error_msg },
        response_headers: response.headers
      )
    end
    
    # Handle other error responses
    status_code = response.body["statusCode"]&.to_i || 0
    if response.body["status"] == false || (status_code > 0 && status_code >= 400)
      error_msg = response.body["message"] || "Authentication failed"
      raise AuthenticationError.new(
        status: status_code,
        body: { "message" => error_msg },
        response_headers: response.headers
      )
    end
  end

  # Store token and expiry time (5 minutes from now)
  if response.body["token"]
    @jwt_token = response.body["token"]
    @jwt_expires_at = Time.now + 300 # 5 minutes = 300 seconds

    # Reset connection to include new Bearer token
    reset_connection! if respond_to?(:reset_connection!, true)
  else
    # No token in response - authentication failed
    raise AuthenticationError, "Authentication failed: No token received. Check your email and password."
  end

  response.body
end

#clear_jwt_tokenvoid

This method returns an undefined value.

Manually clear JWT token (e.g., for logout)



95
96
97
98
# File 'lib/nowpayments/api/authentication.rb', line 95

def clear_jwt_token
  @jwt_token = nil
  @jwt_expires_at = nil
end

#jwt_expired?Boolean

Check if JWT token is expired

Returns:

  • (Boolean)

    True if token is expired or not set



89
90
91
# File 'lib/nowpayments/api/authentication.rb', line 89

def jwt_expired?
  !@jwt_token || !@jwt_expires_at || Time.now >= @jwt_expires_at
end

#jwt_time_remainingInteger?

Get time remaining until JWT token expires

Returns:

  • (Integer, nil)

    Seconds until expiry, or nil if no token



102
103
104
105
106
107
# File 'lib/nowpayments/api/authentication.rb', line 102

def jwt_time_remaining
  return nil unless @jwt_token && @jwt_expires_at

  remaining = (@jwt_expires_at - Time.now).to_i
  remaining.positive? ? remaining : 0
end

#jwt_token(email: nil, password: nil) ⇒ String?

Get current JWT token (refreshes if expired)

Parameters:

  • email (String, nil) (defaults to: nil)

    Email for re-authentication if token expired

  • password (String, nil) (defaults to: nil)

    Password for re-authentication if token expired

Returns:

  • (String, nil)

    Current valid JWT token or nil



80
81
82
83
84
85
# File 'lib/nowpayments/api/authentication.rb', line 80

def jwt_token(email: nil, password: nil)
  # Auto-refresh if expired and credentials provided
  authenticate(email: email, password: password) if jwt_expired? && email && password

  @jwt_token
end