<@U04UQ68083H> how do i initialize the ory api cli...
# ory-copilot
b
@chilly-king-10285 how do i initialize the ory api client in java sdk
b
A message from kapa.ai
To initialize the Ory API client in the Java SDK, you need to use the
Configuration
class to set the base path (your Ory project URL or custom domain), and then instantiate the relevant API class (such as
IdentityApi
). Here’s a basic example:
Copy code
java import sh.ory.client.ApiClient; import sh.ory.client.ApiException; import sh.ory.client.Configuration; import sh.ory.client.api.IdentityApi;  public class OryClientExample {     public static void main(String[] args) {         // Set your Ory project URL or custom domain         String basePath = "https://{your-project}.<http://projects.oryapis.com|projects.oryapis.com>";         ApiClient defaultClient = Configuration.getDefaultApiClient();         defaultClient.setBasePath(basePath);          // Initialize the Identity API         IdentityApi apiInstance = new IdentityApi(defaultClient);          // Now you can use apiInstance to call Ory APIs     } }
Replace
{your-project}
with your actual Ory project slug or use your custom domain if configured. If you need to use an API key for admin endpoints, you can set it on the
ApiClient
as well. For more details, see the [Ory API Reference](https://www.ory.sh/docs/reference/api) and the [Java SDK documentation](https://github.com/ory/sdk/blob/master/clients/client/java/README.md) [Ory API Reference](https://www.ory.sh/docs/reference/api).
b
@chilly-king-10285 you also need the api key and bearer token configured
b
A message from kapa.ai
To initialize the Ory API client in the Java SDK and configure it with an API key (bearer token), you need to set the
Authorization
header with your API key. The knowledge sources provide examples for using API keys with the REST API, but do not include a direct Java SDK code sample. However, the general approach is to set the
Authorization
header to
Bearer <API_KEY>
when making API calls. Here’s how you would do this based on the information from the knowledge sources: 1. Obtain your API key from the Ory Console. It will look like
ory_pat_...
for project admin APIs or
ory_wak_...
for workspace admin APIs [Authorizing Ory Network Workspace Admin APIs](https://www.ory.sh/docs/concepts/personal-access-token#authorizing-ory-network-workspace-admin-apis). 2. Set the API key as a Bearer token in the Authorization header. For REST API calls, the header should be:
Copy code
Authorization: Bearer <API_KEY>
This is required for privileged (admin) API operations [Manage Ory Network projects through the API](https://www.ory.sh/docs/guides/manage-project-via-api). 3. In the Java SDK, you typically set default headers on the
ApiClient
. While the knowledge sources do not provide a Java code example, the equivalent logic is to set the default header for `Authorization`:
Copy code
java import sh.ory.client.ApiClient; import sh.ory.client.Configuration; import sh.ory.client.api.IdentityApi;  public class OryClientExample {     public static void main(String[] args) {         String basePath = "https://{your-project}.<http://projects.oryapis.com|projects.oryapis.com>";         String apiKey = "ory_pat_..."; // Replace with your actual API key          ApiClient defaultClient = Configuration.getDefaultApiClient();         defaultClient.setBasePath(basePath);         defaultClient.addDefaultHeader("Authorization", "Bearer " + apiKey);          IdentityApi apiInstance = new IdentityApi(defaultClient);          // Now you can use apiInstance to call Ory APIs     } }
Note: The knowledge sources do not provide a Java-specific code sample, but this approach follows the documented requirement to use the
Authorization: Bearer <API_KEY>
header for admin API access [Authorizing Ory Network Workspace Admin APIs](https://www.ory.sh/docs/concepts/personal-access-token#authorizing-ory-network-workspace-admin-apis). If you need more details or a Java-specific example from the official documentation, please let me know.