API authentication

|

View as Markdown

Every request to the Infobip API needs two things: the Base URL of your account, and credentials in the Authorization header. Infobip supports four authentication methods: API keys, OAuth 2.0, IBSSO tokens, and HTTP Basic authentication. Use an API key unless your tech stack or security requirements call for another method.

Authentication proves who is calling. What the call is allowed to do depends on the scopes of your credentials: see Authorization and scopes. If a request fails with 401 Unauthorized, see Authentication errors.



Base URL

Every API request must include the Base URL unique to your account, in the format xxxxx.api.infobip.com. To find yours, log in to your account. The Base URL is shown on your homepage and on the API keys page of the web interface, and the API docs display it in every endpoint example.

json
1Base URL: https://example.api.infobip.com
2Base URL used in the Send SMS message endpoint: https://example.api.infobip.com/sms/3/messages
NOTEThe Base URL routes your requests to the nearest data center at the DNS level. It optimizes performance and does not authorize access, so it is not a secret. An incorrect Base URL can degrade or interrupt the service.


Choose an authentication method

Whichever method you use, send requests over an encrypted TLS connection. See REST API traffic encryption.

The following table compares the methods, from most to least recommended, across security, complexity, session lifetime, and ideal use cases.

MethodSecurity levelSetup complexitySession lifetimeBest for
API keysHigh (scoped, revocable)Low. Generate a key and use it directly. Rotate keys regularly.Fixed expiry date you set. Through the API, 1 year by default.Most integrations. Server-to-server calls where you want fine-grained scope control, revocable credentials, and long-lived access without token refresh. The only method the Java and C# API client libraries support.
OAuth 2.0HighestMedium. Request a token before each session.Short-lived, expires in seconds (per response)Integrations that need the most secure, industry-standard flow, or must work with systems that already speak OAuth.
IBSSO tokensHigh (session-based)Medium. Create a session, destroy it when needed.60 minutes by default. Destroy the session to end it sooner.Single sign-on scenarios, or centralizing sensitive data handling across enterprise systems without distributing it.
HTTP Basic authenticationLow (easily decoded)Lowest. No separate token step.None, sent with every requestOnly when no other method is available, for example to create your first API key through the API. Do not use in production.

To troubleshoot authentication issues, see Authentication errors and Handling errors, or contact Support.



API key authentication

An API key is an access token that your application provides when making API calls. It is a simple way to secure access, which makes it the most popular authentication method for REST APIs. Send the key as a request header, using the App prefix: Authorization: App {your-api-key}. Avoid sending it in the query string, where it can leak into server logs and browser history.

Self-signup accounts are automatically assigned an API key after you create an account. Managed accounts do not receive an API key automatically, so generate one before you make your first API call.

  • You can revoke keys at any time, which is useful when separating API access rights across multiple applications or use cases.
  • API keys expire on the date you set. If you create a key through the API without an expiry date, it expires 1 year after creation.
  • Rotate API keys regularly, and immediately if a key is exposed. See Security best practices.
NOTEManaging messaging for multiple businesses or brands: You can scope API keys to specific Applications and Entities. This isolates traffic per business, environment, or brand. See Manage API keys in the CPaaS X docs.

How to use API keys [#how-to-use-api-keys-api-key-authentication]

  1. Get an API key. A self-signup account comes with an API key. To create more, or on other accounts, generate one on the Infobip API key management page (Developer Tools > API Keys). You can also use the Create API key endpoint. The API key endpoints do not accept an API key, so authenticate those calls with Basic authentication, OAuth 2.0, or an IBSSO token.
  2. Assign the scopes your integration needs to the key. See Authorization and scopes for available scopes and how to assign them.
  3. Include App and the key in the Authorization header on every API request: Authorization: App {your-api-key}.

Send a request with an HTTP client [#use-http-client]

The following examples send an SMS message with the Send SMS endpoint, authenticated with an API key. Replace {baseUrl} and {apiKey} with your own values.

HTTP CLIENT
json
1curl -X POST 'https://{baseUrl}/sms/3/messages' \
2 -H 'Authorization: App {apiKey}' \
3 -H 'Content-Type: application/json' \
4 -H 'Accept: application/json' \
5 -d '{
6 "messages": [
7 {
8 "sender": "InfoSMS",
9 "destinations": [{ "to": "41793026727" }],
10 "content": { "text": "This is a sample message" }
11 }
12 ]
13 }'

Send a request with an API client library [#use-an-api-client-library-api-key-authentication]

The API client libraries support API key authentication. Provide your Base URL and API key when you create the client. The library adds the App prefix to the Authorization header for you.

CLIENT LIBRARY SAMPLES
java
1import com.infobip.ApiClient;
2import com.infobip.ApiKey;
3import com.infobip.BaseUrl;
4import com.infobip.api.SmsApi;
5import com.infobip.model.*;
6import java.util.List;
7 
8ApiClient apiClient = ApiClient.forApiKey(ApiKey.from("{apiKey}"))
9 .withBaseUrl(BaseUrl.from("https://{baseUrl}"))
10 .build();
11 
12SmsApi smsApi = new SmsApi(apiClient);
13 
14SmsMessage message = new SmsMessage()
15 .sender("InfoSMS")
16 .addDestinationsItem(new SmsDestination().to("41793026727"))
17 .content(new SmsTextContent().text("This is a sample message"));
18 
19SmsRequest smsRequest = new SmsRequest().messages(List.of(message));
20 
21SmsResponse smsResponse = smsApi.sendSmsMessages(smsRequest).execute();


OAuth 2.0 authentication

OAuth 2.0 is the most secure authentication option and an industry standard, defined in RFC 6749(opens in new tab). You get a short-lived access token from the Create OAuth2 token endpoint and send it with each request.

  • The access token expires within the time limit provided in seconds in the response.
  • Infobip acts as both a resource and an authorization server.
  • You must create a new token after the token expires. There is no automatic token retrieval.

How to use OAuth 2.0 authentication [#how-to-use-oauth-20-authentication-oauth-20-authentication]

  1. Call Create OAuth2 token to get the access token and its expiration time.
  2. Include Bearer and the token in the Authorization header for all subsequent calls: Authorization: Bearer {access_token}.

Get an OAuth 2.0 token [#get-an-oauth-20-token-oauth-20-authentication]

The following examples call the Create OAuth2 token endpoint and read the token from the response. Use your Infobip username as client_id and your password as client_secret. The expires_in field in the response contains the token lifetime in seconds.

OBTAIN TOKEN SAMPLES
bash
1curl -X POST 'https://{baseUrl}/auth/1/oauth2/token' \
2 -H 'Content-Type: application/x-www-form-urlencoded' \
3 -H 'Accept: application/json' \
4 --data-urlencode 'client_id={username}' \
5 --data-urlencode 'client_secret={password}' \
6 --data-urlencode 'grant_type=client_credentials'

Send a request with an OAuth 2.0 token [#send-a-request-with-an-oauth-20-token-oauth-20-authentication]

The following examples assume you have completed step 1 and have an accessToken value. The request is the same as for API key authentication, but uses Bearer instead of App in the header.

SEND SMS WITH AN OAUTH 2.0 TOKEN
json
1curl -X POST 'https://{baseUrl}/sms/3/messages' \
2 -H 'Authorization: Bearer {accessToken}' \
3 -H 'Content-Type: application/json' \
4 -H 'Accept: application/json' \
5 -d '{
6 "messages": [
7 {
8 "sender": "InfoSMS",
9 "destinations": [{ "to": "41793026727" }],
10 "content": { "text": "This is a sample message" }
11 }
12 ]
13 }'
NOTEThe Java and C# API client libraries support API key authentication only. To use OAuth 2.0, send requests with an HTTP client, as shown in the previous examples.


IBSSO token authentication

IBSSO (Infobip single sign-on) tokens are session-based, meaning tokens expire after a short time. That makes this method more secure, but also requires more maintenance to keep authentication active. This method is typically used in single sign-on scenarios.

  • All API requests are authenticated with a session token.
  • By default, IBSSO tokens expire after roughly 60 minutes of inactivity.
  • If you want to create a new token before the previous one expires, you must first destroy the session.
  • To make a session shorter than 60 minutes, destroy it when you no longer need it.

How to use IBSSO tokens [#how-to-use-ibsso-tokens-ibsso-token-authentication]

  1. Call Create session and take the token from the response.
  2. Include IBSSO and the token in the Authorization header: Authorization: IBSSO {token}.
  3. (Optional) Call Destroy session to end the session before it expires.

Get an IBSSO token [#get-an-ibsso-token-ibsso-token-authentication]

The following examples call the Create session endpoint and read the token from the response.

OBTAIN TOKEN SAMPLES
json
1curl -X POST 'https://{baseUrl}/auth/1/session' \
2 -H 'Content-Type: application/json' \
3 -H 'Accept: application/json' \
4 -d '{ "username": "{username}", "password": "{password}" }'

Send a request with an IBSSO token [#send-a-request-with-an-ibsso-token-ibsso-token-authentication]

This request is almost identical to API key authentication. Instead of App, you use IBSSO in the header.

SEND SMS WITH AN IBSSO TOKEN
json
1curl -X POST 'https://{baseUrl}/sms/3/messages' \
2 -H 'Authorization: IBSSO {token}' \
3 -H 'Content-Type: application/json' \
4 -H 'Accept: application/json' \
5 -d '{
6 "messages": [
7 {
8 "sender": "InfoSMS",
9 "destinations": [{ "to": "41793026727" }],
10 "content": { "text": "This is a sample message" }
11 }
12 ]
13 }'
NOTEThe Java and C# API client libraries support API key authentication only. To use IBSSO tokens, send requests with an HTTP client, as shown in the previous examples.


Basic authentication

Basic authentication works by sending a username and a password in every API request, Base64-encoded and passed with the Basic prefix: Authorization: Basic {base64-encoded-credentials}. Use it when an API key is not available. For example, the endpoints that create and manage API keys do not accept an API key.

  • Built into the HTTP protocol itself. See HTTP authentication(opens in new tab) on MDN.
  • Credentials encoded in Base64 format and separated by a colon.
  • Encoded credentials added to the header after Basic.

Unlike API keys, where you explicitly assign scopes during key creation, Basic authentication does not use scopes directly. Instead, the roles assigned to the user whose credentials you are using determine the scopes available to a Basic authentication request. For the role-to-scope mapping, see User roles and API scopes in API scopes reference.

IMPORTANTBasic authentication is the least recommended method: Base64-encoded credentials are easily decoded back to their original values. For production integrations, use API keys or OAuth 2.0.

How to use Basic authentication [#how-to-use-basic-authentication-basic-authentication]

Basic authentication must be enabled for your user. See API access in Manage users.

  1. Concatenate your username and password separated by a colon: username:password.
  2. Encode the result in Base64.
  3. Include Basic and the encoded string in the Authorization header: Authorization: Basic {base64-encoded-credentials}.

Send a request with Basic authentication [#send-a-request-with-basic-authentication-basic-authentication]

The following examples encode the credentials and send them in the Authorization header. The curl example uses already-encoded credentials.

SEND SMS WITH BASIC AUTHENTICATION
json
1curl -X POST 'https://{baseUrl}/sms/3/messages' \
2 -H 'Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==' \
3 -H 'Content-Type: application/json' \
4 -H 'Accept: application/json' \
5 -d '{
6 "messages": [
7 {
8 "sender": "InfoSMS",
9 "destinations": [{ "to": "41793026727" }],
10 "content": { "text": "This is a sample message" }
11 }
12 ]
13 }'
NOTEThe API client libraries support API key authentication only. Some libraries accept a username and password in their configuration, but do not send them with requests. To use Basic authentication, send requests with an HTTP client, as shown in the previous examples.


Authentication errors

You receive the 401 Unauthorized HTTP status code when credentials are missing or incorrect. This applies to all four authentication methods.

Authentication error
1{
2 "errorCode": "E401",
3 "description": "The request lacks valid authentication credentials for the requested resource.",
4 "action": "Check the resources and adjust authentication credentials.",
5 "violations": [],
6 "resources": [
7 {
8 "name": "API Authentication",
9 "url": "/docs/developer-resources/authentication-and-access/api-authentication"
10 }
11 ]
12}

Some endpoints can return a previous version of the error:

Authentication error (legacy format)
1{
2 "requestError": {
3 "serviceException": {
4 "messageId": "UNAUTHORIZED",
5 "text": "Invalid login details"
6 }
7 }
8}

Library exceptions [#library-exceptions-authentication-errors]

When you use an API client library, an error response such as 401 Unauthorized throws an ApiException. The following examples read the status code and response body from the exception. The smsApi and smsRequest objects come from Use an API client library.

EXCEPTION HANDLING SAMPLES
java
1import com.infobip.ApiException;
2import com.infobip.model.SmsResponse;
3 
4try {
5 SmsResponse smsResponse = smsApi.sendSmsMessages(smsRequest).execute();
6} catch (ApiException apiException) {
7 int statusCode = apiException.responseStatusCode();
8 String rawResponseBody = apiException.rawResponseBody();
9 var details = apiException.details();
10}


Related pages

Authorization and scopes
Understand API scopes and how to assign or update them on API keys and user roles.

Security best practices
Guidelines for securing API integrations: authentication, traffic encryption, credential storage, and AIT prevention.

API requests and responses
Conventions, content types, pagination, and bad request handling across the REST API.




Was this page helpful?