Skip to content

Commit e951848

Browse files
Revise connect-to-anonymous-apis documentation (#10712)
* Revise connect-to-anonymous-apis documentation Updated metadata and content for clarity and accuracy. * Revise SharePoint API connection documentation Updated the documentation to reflect new approaches for connecting to SharePoint APIs, including changes to the title, description, and date.
1 parent b1c7b4f commit e951848

2 files changed

Lines changed: 22 additions & 20 deletions

File tree

docs/spfx/connect-to-anonymous-apis.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Connect to anonymous APIs
3-
description: Different approaches of connecting to anonymous APIs from your SharePoint Framework solutions
4-
ms.date: 12/04/2020
3+
description: Different approaches for connecting to anonymous APIs from your SharePoint Framework solutions
4+
ms.date: 03/11/2026
55
ms.localizationpriority: high
66
---
77

@@ -10,11 +10,11 @@ ms.localizationpriority: high
1010
When building SharePoint Framework solutions, you might want to consume public APIs, such as stock or weather information. This article outlines how to connect to public APIs in SharePoint Framework solutions.
1111

1212
> [!NOTE]
13-
> In this article, public and anonymous APIs are used interchangeably. This article is about connecting to APIs, that don't require authentication at all or are secured with a function/API key that can be passed via query string parameters. See other pages in this section of the documentation for more information about connecting to the [SharePoint APIs](connect-to-sharepoint.md) or [APIs secured with Azure AD](use-aadhttpclient.md).
13+
> In this article, public and anonymous APIs are used interchangeably. This article is about connecting to APIs that don't require authentication at all or are secured with a function/API key that can be passed via query string parameters. See other pages in this section of the documentation for more information about connecting to the [SharePoint APIs](connect-to-sharepoint.md) or [APIs secured with Microsoft Entra ID](use-aadhttpclient.md).
1414
1515
## Connect to anonymous APIs using the HttpClient
1616

17-
The easiest way, to connect to anonymous APIs in your SharePoint Framework solutions, is by using the HttpClient provided as a part of the SharePoint Framework. For example, to get dummy information from the Typicode service, you would execute:
17+
The easiest way to connect to anonymous APIs in your SharePoint Framework solutions is by using the HttpClient provided as a part of the SharePoint Framework. For example, to get dummy information from the Typicode service, you would execute:
1818

1919
```typescript
2020
this.context.httpClient
@@ -27,7 +27,7 @@ this.context.httpClient
2727
});
2828
```
2929

30-
Similarly to the **SPHttpClient** you use for connecting to SharePoint APIs, the **HttpClient** offers you similar capabilities for running the most common web requests. If necessary, you can use its options, to configure requests.
30+
Similarly to the **SPHttpClient** you use for connecting to SharePoint APIs, the **HttpClient** offers you similar capabilities for running the most common web requests. If necessary, you can use its options to configure requests.
3131

3232
For example, to specify request headers, you would use the following code:
3333

@@ -57,4 +57,5 @@ While the **HttpClient** is similar to the **SPHttpClient**, it doesn't include
5757

5858
#### Part of the SharePoint Framework
5959

60-
The **HttpClient** is part of the SharePoint Framework and you don't need any additional dependencies to start using it. It is already available on the page that is why using it doesn't cause additional performance overhead on runtime.
60+
The **HttpClient** is part of the SharePoint Framework and you don't need any additional dependencies to start using it. It is already available on the page, which is why using it doesn't cause additional performance overhead on runtime.
61+

docs/spfx/connect-to-sharepoint.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
---
22
title: Connect to SharePoint APIs
3-
description: Different approaches of connecting to SharePoint APIs from your SharePoint Framework solutions
4-
ms.date: 02/24/2022
3+
description: Different approaches for connecting to SharePoint APIs from your SharePoint Framework solutions
4+
ms.date: 03/11/2026
55
ms.localizationpriority: high
66
---
77

88
# Connect to SharePoint APIs
99

10-
In your SharePoint Framework solutions, you'll likely want to interact with data stored in SharePoint. SharePoint offers a rich set of APIs that can be consumed in various ways. This article outlines what options you have, how they work and what their advantages and disadvantages are.
10+
In your SharePoint Framework solutions, you'll likely want to interact with data stored in SharePoint. SharePoint offers a rich set of APIs that can be consumed in various ways. This article outlines what options you have, how they work, and what their advantages and disadvantages are.
1111

1212
## Connect to SharePoint API using the SPHttpClient
1313

14-
SharePoint Framework offers the SPHttpClient that you can use to connect to SharePoint REST APIs. A ready-to-use instance of the SPHttpClient is available on the web part/extension context and you can use it to do all kinds of web request. Following code snippet shows how you would use the SPHttpClient to retrieve the title of the current site:
14+
SharePoint Framework offers the SPHttpClient that you can use to connect to SharePoint REST APIs. A ready-to-use instance of the SPHttpClient is available on the web part/extension context and you can use it to do all kinds of web requests. The following code snippet shows how you would use the SPHttpClient to retrieve the title of the current site:
1515

1616
```typescript
1717
this.context.spHttpClient
@@ -24,7 +24,7 @@ this.context.spHttpClient
2424
});
2525
```
2626

27-
The SPHttpClient offers basic functionality for performing the most common web requests. It allows you also, to configure your request by, for example, specifying request headers. For example, if you wanted to issue a web request without retrieving metadata, you'd use the following code:
27+
The SPHttpClient offers basic functionality for performing the most common web requests. It also allows you to configure your request by, for example, specifying request headers. For example, if you wanted to issue a web request without retrieving metadata, you'd use the following code:
2828

2929
```typescript
3030
this.context.spHttpClient
@@ -49,7 +49,7 @@ When using the SPHttpClient, there are a few things that you should take into ac
4949

5050
#### OData v4.0
5151

52-
By default, the SPHttpClient uses OData v4 specification, which requires using `odata.metadata` instead of just `odata` to control the response metadata. If you use the `odata` directive in OData v4 mode, you'll get an error, like: _The HTTP header ACCEPT is missing or its value is invalid._. It is possible to set the SPHttpClient to OData v3.0 mode, by setting the `odata-version` request header to empty value:
52+
By default, the SPHttpClient uses OData v4 specification, which requires using `odata.metadata` instead of just `odata` to control the response metadata. If you use the `odata` directive in OData v4 mode, you'll get an error, like: _The HTTP header ACCEPT is missing or its value is invalid._. It is possible to set the SPHttpClient to OData v3.0 mode by setting the `odata-version` request header to empty value:
5353

5454
```typescript
5555
this.context.spHttpClient
@@ -71,15 +71,15 @@ this.context.spHttpClient
7171

7272
#### Authentication cookies
7373

74-
In the SharePoint Framework, there are a number of classes for executing web requests. Two of them are the **SPHttpClient** and **HttpClient**. One of the differences between the SPHttpClient and HttpClient is that the SPHttpClient includes authentication cookies when issuing web requests. Because SharePoint APIs are not anonymous, you need to provide authentication information or you'll get an 401 Unauthorized response. Because the HttpClient doesn't include authentication cookies in its request, if you used it to call the SharePoint REST APIs, your requests would fail with a 401 Unauthorized response.
74+
In the SharePoint Framework, there are a number of classes for executing web requests. Two of them are the **SPHttpClient** and **HttpClient**. One of the differences between the SPHttpClient and HttpClient is that the SPHttpClient includes authentication cookies when issuing web requests. Because SharePoint APIs are not anonymous, you need to provide authentication information or you'll get a 401 Unauthorized response. Because the HttpClient doesn't include authentication cookies in its requests, if you used it to call the SharePoint REST APIs, your requests would fail with a 401 Unauthorized response.
7575

7676
#### Part of the SharePoint Framework
7777

78-
The SPHttpClient is part of the SharePoint Framework and you don't need any additional dependencies to start using it. It is already available on the page that is why using it doesn't cause additional performance overhead on runtime.
78+
The SPHttpClient is part of the SharePoint Framework and you don't need any additional dependencies to start using it. It is already available on the page, which is why using it doesn't cause additional performance overhead at runtime.
7979

8080
#### Raw REST queries are error-prone
8181

82-
The SPHttpClient offers basic support for communicating with the SharePoint REST API. If your applications require more complex GET requests, POST requests or uses more advanced capabilities such as batching, you'll quickly notice that using the SPHttpClient is cumbersome and error-prone. In such cases, you should consider using an alternative such as the PnPjs library that offers you a fluent API that can be verified for correctness by TypeScript.
82+
The SPHttpClient offers basic support for communicating with the SharePoint REST API. If your applications require more complex GET requests, POST requests or use more advanced capabilities such as batching, you'll quickly notice that using the SPHttpClient is cumbersome and error-prone. In such cases, you should consider using an alternative such as the PnPjs library that offers you a fluent API that can be verified for correctness by TypeScript.
8383

8484
## Connect to SharePoint using PnPjs
8585

@@ -94,26 +94,27 @@ console.log(web.Title);
9494

9595
[!INCLUDE [pnp-js](../../includes/snippets/open-source/pnp-js.md)]
9696

97-
Notice, how less verbose the code is comparing to the SharePoint Framework SPHttpClient and how all elements of the requests, except for the names of the properties to retrieve, are strongly typed lowering the risk of runtime errors.
97+
Notice how much less verbose the code is compared to the SharePoint Framework SPHttpClient and how all elements of the requests, except for the names of the properties to retrieve, are strongly typed lowering the risk of runtime errors.
9898

9999
For more information about how to setup and use PnPjs in the SharePoint Framework, see the PnPjs documentation at [https://pnp.github.io/pnpjs/](https://pnp.github.io/pnpjs/).
100100

101101
### Considerations for using PnPjs
102102

103103
When deciding whether you should use PnPjs or not, following are a few considerations that you should take into account.
104104

105-
#### Raw REST queries are error prone
105+
#### Raw REST queries are error-prone
106106

107-
Issuing raw REST requests using the SPHttpClient is error-prone. Especially, when your application will need to execute POST queries or you'll want to use some of the more advanced capabilities such as request batching, composing the correct requests and parsing the responses will be cumbersome. Not to mention, the only way to verify if the requests are correct is by running the code in the browser. With PnPjs, you can communicate with SharePoint APIs in a type-safe way and easily use the advanced capabilities of SharePoint API what allows you to focus on building your application instead of testing its requests.
107+
Issuing raw REST requests using the SPHttpClient is error-prone. Especially when your application needs to execute POST queries or you'll want to use some of the more advanced capabilities such as request batching, composing the correct requests and parsing the responses will be cumbersome. Not to mention, the only way to verify if the requests are correct is by running the code in the browser. With PnPjs, you can communicate with SharePoint APIs in a type-safe way and easily use the advanced capabilities of the SharePoint API, which allows you to focus on building your application instead of testing its requests.
108108

109109
#### Open-source project
110110

111111
PnPjs is an open-source project managed by the SharePoint community. There's no SLA for using PnPjs in your solutions and Microsoft support won't assist you with any possible issues when caused by PnPjs. That said, PnPjs is actively developed and the community quickly responds to all submitted issues and questions.
112112

113113
#### Additional dependency
114114

115-
PnPjs is an additional dependency that you need to add to your project and manage over time. You need to keep track of its updates and upgrade your project when necessary. The community behind PnPjs regularly communicates the state of the current work, upcoming releases, and the impact, if any, of upgrading to the newer version.
115+
PnPjs is an additional dependency that you need to add to your project and manage over time. You need to keep track of its updates and upgrade your project when necessary. The community behind PnPjs regularly communicates the state of the current work, upcoming releases, and the impact, if any, of upgrading to a newer version.
116116

117117
#### Additional payload
118118

119-
PnPjs offers a rich set of capabilities for communicating with SharePoint APIs. The library supports selective imports so with carful curation the overall impact to your bundle size can be mitigated. For more information please see the documentation at [https://pnp.github.io/pnpjs/](https://pnp.github.io/pnpjs/).
119+
PnPjs offers a rich set of capabilities for communicating with SharePoint APIs. The library supports selective imports so with careful curation the overall impact to your bundle size can be mitigated. For more information, please see the documentation at [https://pnp.github.io/pnpjs/](https://pnp.github.io/pnpjs/).
120+

0 commit comments

Comments
 (0)